题目地址:
https://www.acwing.com/problem/content/17/
请实现一个函数,把字符串
s
s
s中的每个空格替换成"%20"
。你可以假定输入字符串的长度最大是
1000
1000
1000。注意输出字符串的长度可能大于
1000
1000
1000。
代码如下:
using namespace std;
class Solution {
public:
string replaceSpaces(string &str) {
string res;
for (int i = 0; i < str.size(); i++)
if (str[i] == ' ') res += "%20";
else res += str[i];
return res;
}
};
时间复杂度 O ( l s ) O(l_s) O(ls)。