leetcode/src/offer_05.cpp
2022-11-29 20:26:23 +08:00

25 lines
485 B
C++

#include "offer_05.hpp"
string Solution::replaceSpace(string s) {
int len = s.length();
int cnt{0};
for (int i{0}; i < len; ++i) {
if (s[i] == ' ') {
s.push_back(' ');
s.push_back(' ');
cnt++;
}
}
len = s.length();
for (int fast = len - 1, slow = len - 1 - 2 * cnt; fast >= 0; --slow) {
if (s[slow] == ' ') {
s[fast--] = '0';
s[fast--] = '2';
s[fast--] = '%';
} else {
s[fast--] = s[slow];
}
}
return s;
}