-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetcode第151题.cpp
More file actions
46 lines (41 loc) · 1.03 KB
/
Leetcode第151题.cpp
File metadata and controls
46 lines (41 loc) · 1.03 KB
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
41
42
43
44
45
46
class Solution {
public:
void reverse(string &s,int start,int end)
{
for (int i = start, j = end; i < j; i++, j--) {
swap(s[i], s[j]);
}
}
void removeExtraSpaces(string &s)
{
int slow=0;
for(int i=0;i<s.size();++i)
{
if(s[i]!=' '){
if(slow>0){s[slow++]=' ';}
while(i<s.size()&&s[i]!=' ')
{
s[slow++]=s[i++];
}
}
}
s.resize(slow);
}
string reverseWords(string s) {
removeExtraSpaces(s);
reverse(s,0,s.size()-1);
for(int i=0,j=0;i<=s.size();++i)
{
if(s[i]==' '||i==s.size())
{
reverse(s,j,i-1);
j=i+1;
}
}
return s;
}
};
//python调库比较无赖,就放在这边好了(看不见我,看不见我)
// class Solution(object):
// def reverseWords(self, s):
// return " ".join(s.split()[::-1])