leetcode-709-To-Lower-Case

描述


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

Example 1:

1
2
Input: "Hello"
Output: "hello"

Example 2:

1
2
Input: "here"
Output: "here"

Example 3:

1
2
Input: "LOVELY"
Output: "lovely"

分析


使用 Java 来解决的话,需要先把字符串转换为字符数组,遍历字符数组,把大写字母转换为小写字母

解决方案1(Java)


1
2
3
4
5
6
7
8
9
10
11
class Solution {
public String toLowerCase(String str) {
char[] result = str.toCharArray();
for (int i = 0; i < result.length; i++) {
if ('A' <= result[i] && result[i] <= 'Z') {
result[i] = (char) (result[i] - 'A' + 'a');
}
}
return new String(result);
}
}

题目来源