class Solution {
public:
string replaceSpaces(string S, int length) {
string res; int i = 0;
for (int i = 0; i < length ; i ++)
if (S[i] == ' '){
res +="%20";
}
else{
res += S[i];
}
return res;
}
};
这个题把res +="%20";
改成res +res +"%20"
;就会超时。最好是在原来的string上移动加修改,字符搬动越少越好,字符复制等比较耗时。