leetcode709. To Lower Case

Example 1:

Input: "Hello"
Output: "hello"
Example 2:

Input: "here"
Output: "here"
Example 3:

Input: "LOVELY"
Output: "lovely"

1、ord() and char()

class Solution:
    def toLowerCase(self, str):
        """
        """
        output = ''
        for char in str:
            if (65 <= ord(char) <= 90):
                output += chr(ord(char)+32)
            else:
                output += char
        return output

2、create dictionary mappings

import string
class Solution():
    def toLowerCase(self, str):
        """
        """
        mappings = dict(zip(string.ascii_uppercase, string.ascii_lowercase))

        output = ''
        for char in str:
            if char in mappings:
                output += mappings[char]
            else:
                output += char
        return output

3、Built-in lower()

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

猜你喜欢

转载自blog.csdn.net/weixin_38246633/article/details/82870195
今日推荐