Skip to content

Commit d90c06a

Browse files
authored
Create reverse-words-in-a-string-iii.cpp
1 parent 3fcbeb0 commit d90c06a

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

C++/reverse-words-in-a-string-iii.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
string reverseWords(string s) {
7+
for (int i = 0, j = 0; j <= s.length(); ++j) {
8+
if (j == s.length() || s[j] == ' ') {
9+
reverse(s.begin() + i, s.begin() + j);
10+
i = j + 1;
11+
}
12+
}
13+
return s;
14+
}
15+
};

0 commit comments

Comments
 (0)