题目描述
给你一个字符串 s
,将该字符串中的大写字母转换成相同的小写字母,返回新的字符串。
样例
输入:s = "Hello"
输出:"hello"
输入:s = "here"
输出:"here"
输入:s = "LOVELY"
输出:"lovely"
限制
1 <= s.length <= 100
s
由 ASCII 字符集中的可打印字符组成。
算法
(模拟) $O(n)$
- 直接模拟。
时间复杂度
- 遍历字符串一次,故时间复杂度为 $O(n)$。
空间复杂度
- 仅需要常数的额外空间。
C++ 代码
class Solution {
public:
string toLowerCase(string s) {
for (char &c : s)
if ('A' <= c && c <= 'Z')
c = c - 'A' + 'a';
return s;
}
};