-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path42.翻转单词顺序
More file actions
41 lines (40 loc) · 1010 Bytes
/
42.翻转单词顺序
File metadata and controls
41 lines (40 loc) · 1010 Bytes
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
class Solution {
public:
string ReverseSentence(string str) {
if(str.empty()) return str;
reverse(str.begin(), str.end());
string::iterator start = str.begin();
string::iterator end = str.begin();
while(start != str.end()){
while(*end != ' ' && end != str.end()){
++end;
}
reverse(start, end);
if(*end == ' ') ++end;
start = end;
}
return str;
/*
string res = "";
int n = str.size();
if(n == 0) return res;
stack<string> s;
for(int i = 0; i < n; ++i){
if(str[i] == ' '){
s.push(res);
s.push(" ");
res = "";
}
else res += str[i];
}
if(res != "") s.push(res);
res = "";
n = s.size();
while(n--){
res += s.top();
s.pop();
}
return res;
*/
}
};