leetcode 709. To Lower Case(python)

313 阅读1分钟

描述

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

Example 1:

Input: "Hello"
Output: "hello"

Example 2:

Input: "here"
Output: "here"

Example 3:

Input: "LOVELY"
Output: "lovely"

解析

根据题意,遍历每个字符,如果 ascii 大于等于 65 小于等于 90 ,则是大写字母,需要转换为小写字母,其他情况则是小写字母直接使用,拼接起来就是小写字符串结果。

解答

class Solution(object):
    def toLowerCase(self, str):
        """
        :type str: str
        :rtype: str
        """
        result = ""
        for c in str:
            if 65 <=ord(c) <= 90:
                result += chr(ord(c)+32)
            else:
                result += c
        return result
        	      
		

运行结果

Runtime: 8 ms, faster than 99.61% of Python online submissions for To Lower Case.
Memory Usage: 13.6 MB, less than 29.94% of Python online submissions for To Lower Case.

原题链接:leetcode.com/problems/to…

您的支持是我最大的动力