这道题可以用api也可以自己实现,都不难,大小字母之前相差了32,检查到大写字母时加上32即可
python
class Solution(object):
def toLowerCase(self, s):
"""
:type s: str
:rtype: str
"""
return s.lower()
class Solution {
public:
string toLowerCase(string s) {
for (char &c: s) {
if (c >= 'A' && c <= 'Z') c = c - 'A' + 'a';
}
return s;
}
};
题解的方法总让我大开眼界,值得学习!
cpp
class Solution {
public:
string toLowerCase(string s) {
for (char& ch: s) {
if (ch >= 65 && ch <= 90) {
ch |= 32;
}
}
return s;
}
};