leetcode/src/offer_05.cpp
2022-11-30 18:20:36 +08:00

25 lines
484 B
C++

#include "offer_05.hpp"
string Offer05::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;
}