leetcode/src/offer_05.cpp

25 lines
485 B
C++
Raw Normal View History

2022-11-29 12:26:23 +00:00
#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;
}