Skip to content

Instantly share code, notes, and snippets.

@icameling
Created July 19, 2022 04:15
Show Gist options
  • Save icameling/9fd51694979aa8fd85c94c73880b6dc6 to your computer and use it in GitHub Desktop.
Save icameling/9fd51694979aa8fd85c94c73880b6dc6 to your computer and use it in GitHub Desktop.
#字符串 #替换空格
class Solution {
public:
string replaceSpace(string s) {
int space_cnt = count(s.begin(), s.end(), ' ');
if (space_cnt < 0) return s;
int lft = s.size() - 1;
int rht = s.size() + space_cnt * 2 - 1;
s.resize(s.size() + space_cnt * 2);
for (; lft >= 0; lft--) {
if (s[lft] != ' ') {
s[rht] = s[lft];
rht--;
} else {
s[rht] = '0';
s[rht-1] = '2';
s[rht-2] = '%';
rht -= 3;
}
}
return s;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment