-
Notifications
You must be signed in to change notification settings - Fork 1
/
151. Reverse Words in a String.cpp
56 lines (53 loc) · 1.56 KB
/
151. Reverse Words in a String.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class Solution {
public:
string reverseWords(string s) {
// ************** O(n) time and space *****************
/*
int n = s.size();
int flag = 0, isF = 1;
string str = "", ans = "";
for(int i = 0; i < n; i++) {
if(s[i] != ' ' && flag == 0) {
str = s[i];
flag = 1;
}
else if(s[i] != ' ' && flag == 1)
str += s[i];
else {
if(str.size() > 0) {
if(isF == 1) { ans = str; isF = 0;}
else ans = str + " " + ans;
}
str = "";
}
}
if(!str.empty())
isF == 1? ans = str: ans = str + " " + ans;
return ans;
*/
// ************* Inplace Solution O(n) Time, O(1) space *****************
reverse(s.begin(), s.end());
int flag = 0;
for(int i = 0; i < s.size(); i++) {
if(s[i] == ' ') {
if(flag == 0) flag = 1;
else {
s.erase(i, 1);
i--;
}
}
else flag = 0;
}
int pos = 0;
while(s[pos] == ' ') s.erase(pos, 1);
pos = s.size() - 1;
while(s[pos] == ' ') s.erase(pos, 1);
for(int i = 0; i < s.size(); i++) {
int j = i;
while(j+1 < s.size() && s[j+1] != ' ') j++;
reverse(s.begin()+i, s.begin()+j+1);
i = j+1;
}
return s;
}
};