面试题 01.03
Solutions
class Solution {
public:
string replaceSpaces(string S, int length) {
int len = 0;
for (int i = 0; i < length; i++)
len += S[i] == ' ' ? 3 : 1;
int r = length - 1, w = len - 1;
while (r >= 0) {
if (S[r] == ' ') {
S[w--] = '0';
S[w--] = '2';
S[w--] = '%';
}
else
S[w--] = S[r];
r--;
}
return S.substr(0, len);
}
};Last updated