给你一个字符串 s
,将该字符串中的大写字母转换成相同的小写字母,返回新的字符串。
示例 1:
输入:s = "Hello" 输出:"hello"
示例 2:
输入:s = "here" 输出:"here"
示例 3:
输入:s = "LOVELY" 输出:"lovely"
提示:
1 <= s.length <= 100
s
由 ASCII 字符集中的可打印字符组成
遍历字符串,遇到大写的字符,转小写。
class Solution:
def toLowerCase(self, s: str) -> str:
return ''.join([chr(ord(c) | 32) if ord('A') <= ord(c) <= ord('Z') else c for c in s])
class Solution {
public String toLowerCase(String s) {
char[] chars = s.toCharArray();
for (int i = 0; i < chars.length; ++i) {
if (chars[i] >= 'A' && chars[i] <= 'Z') {
chars[i] |= 32;
}
}
return new String(chars);
}
}
class Solution {
public:
string toLowerCase(string s) {
for (char& c : s)
if (c >= 'A' && c <= 'Z')
c |= 32;
return s;
}
};
func toLowerCase(s string) string {
sb := &strings.Builder{}
sb.Grow(len(s))
for _, c := range s {
if c >= 'A' && c <= 'Z' {
c |= 32
}
sb.WriteRune(c)
}
return sb.String()
}
impl Solution {
pub fn to_lower_case(s: String) -> String {
s.to_ascii_lowercase()
}
}
impl Solution {
pub fn to_lower_case(s: String) -> String {
String::from_utf8(
s.as_bytes()
.iter()
.map(|&c| c + if c >= b'A' && c <= b'Z' { 32 } else { 0 })
.collect(),
)
.unwrap()
}
}