forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
57 lines (47 loc) · 1.32 KB
/
main.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
54
55
56
57
/// 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;
/// Using a Stack to reverse the input
/// Time Complexity: O(len(s))
/// Space Complexity: O(len(s))
class Solution {
public:
void reverseWords(vector<char>& str) {
vector<string> stack;
int start = 0;
string cur = "";
for(int i = start; i <= str.size() ;)
if(i == str.size() || str[i] == ' '){
stack.push_back(cur);
start = i + 1;
i = start;
cur = "";
}
else
cur += str[i++];
int index = 0;
for(int i = stack.size() - 1; i >= 0 ; i --){
for(int j = 0; j < stack[i].size() ; j ++)
str[index++] = stack[i][j];
if(i > 0)
str[index++] = ' ';
}
}
};
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;
}