面试题 01.03. URL化
解法:
1 class Solution { 2 public: 3 string replaceSpaces(string S, int length) { 4 int tt = -1 ,rt = 0; 5 string res; 6 while(length--){ 7 if(S[++tt] == ' ') { 8 res.insert(rt,"%20"),rt+=3; 9 //res[rt++] = '%'; 10 //res[rt++] = '2'; 11 //res[rt++] = '0'; 12 } 13 else //res[rt++] = S[tt]; 14 res.push_back(S[tt]),++rt; 15 } 16 //std:: string res(s.begin(),s.end()); 17 return res; 18 } 19 };
注意检测运行结果。
string s = 1;
头插字符/字符串:
s = "4" + s;
尾插字符:
s.push_back('%');
i位置插字符串:
s.append(i,"abc"),i+=3;
浙公网安备 33010602011771号