1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| class Solution { public: string replaceSpaces(string S, int length) { int spaceNum = 0; for(int i = 0; i < length; ++i) spaceNum += (S[i] == ' ' ? 1 : 0); int realSize = length + 2 * spaceNum; S.resize(realSize); for(int i = length - 1, j = realSize - 1; i >= 0; --i, --j) { if(S[i] != ' ') S[j] = S[i]; else { S[j] = '0'; S[j - 1] = '2'; S[j - 2] = '%'; j -= 2; } } return S; } };
|