-
Notifications
You must be signed in to change notification settings - Fork 1
/
151+removeExtraSpace.cpp
40 lines (37 loc) · 1.26 KB
/
151+removeExtraSpace.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Solution {
public:
void removeExtraSpace(string &s) {
int slowIndex = 0, fastIndex = 0;
while (s[fastIndex] == ' ' && fastIndex < s.size()) {
fastIndex++;
}
// 去除中间多余的空格 和 末尾多余的空格
for (; fastIndex < s.size(); fastIndex++) {
//如果当前是空格,前一位置也是空格,什么也不操作,继续循环
if (s[fastIndex] == ' ' && s[fastIndex] == s[fastIndex - 1] ) {
continue;
} else {
s[slowIndex++] = s[fastIndex];
}
}
// slowIndex - 1 指向string的最后的一位置
if (s[slowIndex - 1] == ' ') {
s.resize(slowIndex - 1);
} else {
s.resize(slowIndex);
}
}
string reverseWords(string s) {
removeExtraSpace(s);
reverse(s.begin(), s.end());
int left = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] == ' ') {
reverse(s.begin() + left, s.begin() + i);
left = i + 1; //i为空格, i+1为字母
}
}
reverse(s.begin() + left, s.end()); //由于最后没有空格,所以要单独在反转一次
return s;
}
};