1
class Solution {
2
public:
3
string reverseWords(string s) {
4
int i = 0;
5
string res = "";
6
while (i < s.length()) {
7
while (i < s.length() && s[i] == ' ') i++;
8

9
if (i >= s.length()) break;
10

11
int j = i + 1;
12

13
while (j < s.length() && s[j] != ' ') j++;
14

15
string tmp = s.substr(i, j - i);
16
if (res.length() == 0) {
17
res = tmp;
18
} else {
19
res = tmp + " " + res;
20
}
21
i = j + 1;
22
}
23
return res;
24
}
25
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0