-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay15.cpp
31 lines (30 loc) · 938 Bytes
/
Day15.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
class Solution {
public:
string reverseWords(string s) {
string result;
int index=0, index2;
if(s == "") return s;
result = "";
index = s.find_last_not_of(' ');
if(index == -1) return "";
else s.erase(index + 1);
index = 0;
while(index >= 0){
index = s.find_first_not_of(' ', index);
if(index != -1){
index2 = s.find(' ', index);
if(result != ""){
result.insert(0, " ");
}
result = s.substr(index, index2 - index) + result;
if(index2 >= 0){
index = index2;
}
else{
return result;
}
}
}
return result;
}
};