-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path186*.cpp
37 lines (31 loc) · 875 Bytes
/
186*.cpp
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
// 186. Reverse Words in a String II - https://leetcode.com/problems/reverse-words-in-a-string-ii
#include "bits/stdc++.h"
#include "gtest/gtest.h"
using namespace std;
class Solution {
public:
string reverseWords(string s) {
reverse(s.begin(), s.end());
int n = (int)s.length(), L = 0, R = 0;
while (R < n) {
while (R < n && !isspace(s[R])) { R += 1; };
reverse(s.begin() + L, s.begin() + R);
R += 1;
L = R;
}
return s;
}
};
TEST(SolutionTest, Small) {
Solution sol;
EXPECT_EQ("blue is sky the", sol.reverseWords("the sky is blue"));
}
TEST(SolutionTest, Empty) {
Solution sol;
EXPECT_EQ("", sol.reverseWords(""));
}
int main(int argc, char **argv) {
ios::sync_with_stdio(false);
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}