-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path151.cpp
35 lines (33 loc) · 850 Bytes
/
151.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
class Solution {
public:
string reverseWords(string s) {
int start,end;
int k;
for ( k = 0; k < s.size() && ' ' == s[k]; k++){
;
}
start = k;
for ( k = s.size() - 1; k >= 0 && ' ' == s[k]; k--){
;
}
end = k;
int len;
for ( k = start, len = 0; k <= end; k++) {
if (' ' == s[k] && ' ' == s[k - 1])
continue;
s[len++] = s[k];
}
s.erase(s.begin() + len, s.end());
reverse(s.begin(),s.end());
int n=s.length();
int pos=0;
for(int i=0;i<n;i++){
if(s[i]==' '){
reverse(s.begin()+pos,s.begin()+i);
pos=i+1;
}
}
reverse(s.begin() + pos, s.end());
return s;
}
};