Skip to content

Commit 4f9f9bf

Browse files
committed
3
1 parent d5d2eee commit 4f9f9bf

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

ReverseWordsInAString/ReverseWordsInAString.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,22 @@ class Solution {
4545
}
4646
}
4747
};
48+
49+
// solution 2
50+
class Solution {
51+
public:
52+
void reverseWords(string &s) {
53+
string result;
54+
for (int i = 0, start = 0; i < s.size(); i++) {
55+
if (s[i] == ' ') {
56+
if (i > start) {
57+
result = s.substr(start, i - start) + " " + result;
58+
}
59+
start = i + 1;
60+
} else if (i == s.size() - 1) {
61+
result = s.substr(start, i + 1 - start) + " " + result;
62+
}
63+
}
64+
s = result.substr(0, result.size() - 1);
65+
}
66+
};

0 commit comments

Comments
 (0)