-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path151.cpp
More file actions
48 lines (46 loc) · 931 Bytes
/
151.cpp
File metadata and controls
48 lines (46 loc) · 931 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
42
43
44
45
46
47
48
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
Solution()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
string reverseWords(string s)
{
int j = 0;
while (s[0] == ' ')
{
s.erase(0, 1);
}
while (s[s.size() - 1] == ' ')
{
s.erase(s.size() - 1, 1);
}
while (j < s.length())
{
while (s[j] == ' ' && s[j + 1] == ' ')
{
s.erase(j + 1, 1);
}
j++;
}
int old = s.size() - 1;
j = old;
string res = "";
while (j >= 0)
{
if (s[j] == ' ')
{
res += s.substr(j + 1, old - j) + " ";
old = j - 1;
}
j--;
}
res += s.substr(0, old + 1);
return res;
}
};