[LeetCode] 709. To Lower Case

linspiration

Problem

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"

Solution

class Solution {
    public String toLowerCase(String str) {
        char[] chs = str.toCharArray();
        for (int i = 0; i < chs.length; i++) {
            char ch = chs[i];
            if ((int)ch >= 65 && (int)ch <= 90) chs[i] += 32;
        }
        return new String(chs);
    }
}
//a: 97, z: 122
//A: 65, Z: 90
阅读 2.1k

Road to Glory
對酒當歌,人生幾何? 譬如朝露,去日苦多。
161 声望
53 粉丝
0 条评论
161 声望
53 粉丝
文章目录
宣传栏