LeetCode 709 To Lower Case 解题报告

题目要求

Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase.

题目分析及思路

题目要求返回一个字符串的小写形式。可以直接使用lower()函数。​如果考虑具体的实现逻辑,则将大写字符转成小写字符即可,判断字符是否处在‘A’~‘Z’之间,如果是的话,就把它转成小写字符。

python代码​

class Solution:

    def toLowerCase(self, str):

        """

        :type str: str

        :rtype: str

        """

        res = ""

        for s in str:

            if ord(s)>=ord('A') and ord(s)<=ord('Z'):

扫描二维码关注公众号,回复: 4738402 查看本文章

                res += chr(ord(s)-ord('A')+ord('a'))

            else:

                res += s

        return res

        

猜你喜欢

转载自www.cnblogs.com/yao1996/p/10205676.html
今日推荐