LeetCode Python算法算法提高之LeetCode刷题

709. To Lower Case

2018-07-17  本文已影响0人  fred_33c7

题目地址:https://leetcode.com/problems/to-lower-case/description/
大意:单词或句子里面的大写字母转化为小写字母

思路:各个语言都包装好了这种方法。不用的用ascii码转换也行

# 709. To Lower Case

class Solution:
    def toLowerCase(self, str):
        """
        :type str: str
        :rtype: str
        """
        return str.lower()

    def toLowerCase2(self, str):
        """
        :type str: str
        :rtype: str
        """
        # return [chr(ord(i) + 32) for i in str if 65 <= ord(i) <= 90]
        result = ''
        for i in str:
            index = ord(i)
            if 65 <= index <= 90:
                result += chr(index + 32)
            else:
                result += i
        return result

a = Solution()
print(a.toLowerCase2('Hello'))
上一篇下一篇

猜你喜欢

热点阅读