Created
July 19, 2022 04:15
-
-
Save icameling/9fd51694979aa8fd85c94c73880b6dc6 to your computer and use it in GitHub Desktop.
#字符串 #替换空格
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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