Skip to content

Latest commit

 

History

History
111 lines (62 loc) · 1.77 KB

File metadata and controls

111 lines (62 loc) · 1.77 KB

中文文档

Description

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"

Solutions

Python3

class Solution:
    def toLowerCase(self, str: str) -> str:
        if not str:
            return str
        n = len(str)
        res = []
        for i in range(n):
            c = ord(str[i])
            if c >= 65 and c <= 90:
               c += 32
            res.append(chr(c))
        return ''.join(res)

Java

class Solution {
    public String toLowerCase(String str) {
        int n;
        if (str == null || (n = str.length()) == 0) return str;
        char[] chars = str.toCharArray();
        for (int i = 0; i < chars.length; ++i) {
            boolean isUpper = chars[i] >= 'A' && chars[i] <= 'Z';
            if (isUpper) chars[i] += 32;
        }
        return new String(chars);
    }
}

...