forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main2.cpp
53 lines (43 loc) · 1.18 KB
/
main2.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/// Source : https://leetcode.com/problems/reverse-words-in-a-string-ii/description/
/// Author : liuyubobobo
/// Time : 2018-08-12
#include <iostream>
#include <vector>
using namespace std;
/// Reverse and then Reverse
/// Time Complexity: O(len(s))
/// Space Complexity: O(1)
class Solution {
public:
void reverseWords(vector<char>& str) {
reverse(str, 0, str.size() - 1);
int start = 0;
for(int i = start + 1; i <= str.size() ;)
if(i == str.size() || str[i] == ' '){
reverse(str, start, i - 1);
start = i + 1;
i = start + 1;
}
else
i ++;
}
private:
void reverse(vector<char>& s, int start, int end){
for(int i = start, j = end; i < j; i ++, j --)
swap(s[i], s[j]);
}
};
void print_vec(const vector<char>& vec){
for(char c: vec)
cout << c << " ";
cout << endl;
}
int main() {
vector<char> str1 = {'t', 'h', 'e', ' ',
's', 'k', 'y', ' ',
'i', 's', ' ',
'b', 'l', 'u', 'e'};
Solution().reverseWords(str1);
print_vec(str1);
return 0;
}