forked from larissalages/code_problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path151.cpp
More file actions
51 lines (51 loc) · 991 Bytes
/
151.cpp
File metadata and controls
51 lines (51 loc) · 991 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
49
50
51
class Solution
{
public:
string reverseWords(string a)
{
int i = 0, n;
string st = "";
vector<string> wd;
for (i = a.length() - 1; i >= 0; i--)
{
if (a[i] != ' ')
{
break;
}
}
n = i + 1;
for (i = 0; i < n; i++)
{
if (a[i] != ' ')
{
break;
}
}
for (; i < n; i++)
{
if (a[i] == ' ')
{
if (a[i - 1] != ' ')
{
wd.push_back(st);
st = "";
}
}
else
{
st += a[i];
}
}
wd.push_back(st);
st = "";
for (i = wd.size() - 1; i >= 0; i--)
{
st += wd[i];
if (i != 0)
{
st += " ";
}
}
return st;
}
};