英文题目:
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"
中文题目解释:
实现具有字符串参数str的函数ToLowerCase(),并以小写形式返回相同的字符串。
解析:
将'A'到'Z'之间的字符替换为小写,毕竟不好记清具体Unicode 编码的值,所以 加上 'a'-'A'的差值即可。
结果提交:
class Solution {
public String toLowerCase(String str) {
char[] chars=str.toCharArray();
for (int i=0;i<chars.length;i++) {
if(chars[i]>='A'&&chars[i]<='Z'){
chars[i]=(char)(chars[i]+'a'-'A');
}
}
return new String(chars);
}
}