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"
大小写转换的题,很简单
Python解法
class Solution:
def toLowerCase(self, str: str) -> str:
return str.lower()
Java代码如下:
class Solution {
public String toLowerCase(String str) {
return str.toLowerCase();
}
}
函数说明:
public String toLowerCase()
Converts all of the characters in this String to lower case using the rules of the default locale. This is equivalent to calling toLowerCase(Locale.getDefault()).
Note: This method is locale sensitive, and may produce unexpected results if used for strings that are intended to be interpreted locale independently. Examples are programming language identifiers, protocol keys, and HTML tags. For instance, "TITLE".toLowerCase() in a Turkish locale returns "t\u0131tle", where '\u0131' is the LATIN SMALL LETTER DOTLESS I character. To obtain correct results for locale insensitive strings, use toLowerCase(Locale.ENGLISH).
Returns:
the String, converted to lowercase.
See Also:
toLowerCase(Locale)
Java的正规解法:
class Solution {
public String toLowerCase(String str) {
char[] a = str.toCharArray();
for (int i = 0; i < a.length; i++) {
if ('A' <= a[i] && a[i] <= 'Z') {
a[i] = (char) (a[i] - 'A' + 'a');
}
}
return new String(a);
}
}

博客围绕LeetCode上的字符串大小写转换题目展开,要求实现ToLowerCase()函数将字符串转换为小写。文中给出了Python解法和Java代码,还提及了Java的正规解法,整体题目难度较简单。
794

被折叠的 条评论
为什么被折叠?



