1. 程式人生 > >leetcode709. To Lower Case

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()