709. To Lower Case
Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase.
题目很简单,说的就是英文字符串,转换成小写。
Example 1:
1 | Input: "Hello" |
Example 2:
1 | Input: "here" |
Example 3:
1 | Input: "LOVELY" |
方案一
首先想到的就是遍历字符串,判断每一个字符是否是大写,如果是大写,则按照ASCII码,转换成小写后,添加到一个新的字符串里;
1 | class Solution { |
result
Runtime: 4 ms, faster than 100.00% of C++ online submissions for To Lower Case.
Memory Usage: 8.3 MB, less than 50.42% of C++ online submissions for To Lower Case.
结果看起来还可以,内存占用有点高,想着怎么优化一下;
方案二
string out,占用了一个新建的字符串,是不是可以在原来的字符串里面修改?try try see.
1 | class Solution { |
result
Runtime: 4 ms, faster than 100.00% of C++ online submissions for To Lower Case.
Memory Usage: 8 MB, less than 99.17% of C++ online submissions for To Lower Case.
内存占用降低了点,效果还可以。