05.替换空格
请实现一个函数,把字符串 s 中的每个空格替换成"%20"。
示例 1:
输入:s = “We are happy.”
输出:“We%20are%20happy.”
class Solution {
public:
string replaceSpace(string s) {
string res;
int size = s.size();
for(int i = 0; i < size; i++)
{
if(s[i] == ' '){
res += "%20";
}
else{
res.push_back(s[i]);
}
}
return res;
}
};
58.左旋字符串
字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。请定义一个函数实现字符串左旋转操作的功能。比如,输入字符串"abcdefg"和数字2,该函数将返回左旋转两位得到的结果"cdefgab"。
示例 1:
输入: s = “abcdefg”, k = 2
输出: “cdefgab”
示例 2:
输入: s =“lrloseumgh”, k = 6
输出: “umghlrlose”
class Solution {
public:
string reverseLeftWords(string s, int n) {
int size = s.size();
s += s;
return s.substr(n,size);;
}
};