Skip to content

Commit f041fc9

Browse files
authored
Create ReverseWords in a StringIII.cpp
1 parent 389d6ff commit f041fc9

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

easy/ReverseWords in a StringIII.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//557. Reverse Words in a String III
2+
class Solution {
3+
public:
4+
string reverseWords(string s) {
5+
int i = 0;
6+
int j = 0;
7+
8+
while (i < s.length()) {
9+
while (i < j || i < s.length() && s[i] == ' ')
10+
++i;
11+
while (j < i || j < s.length() && s[j] != ' ')
12+
++j;
13+
reverse(begin(s) + i, begin(s) + j);
14+
}
15+
16+
return s;
17+
}
18+
};

0 commit comments

Comments
 (0)