-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path151.cpp
More file actions
41 lines (38 loc) · 780 Bytes
/
Copy path151.cpp
File metadata and controls
41 lines (38 loc) · 780 Bytes
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
#include<iostream>
#include<vector>
#include<sstream>
using namespace std;
class Solution {
public:
void reverseWords(string &s) {
if(s.empty())
return;
stringstream ss(s);
string temp;
vector<string> v;
while(ss >> temp) {
v.push_back(temp);
}
if(v.size() == 0){
s = "";
return ;
}
for(int i=0, j=v.size()-1; i <= j; i++, j--){
string aa = v[i];
v[i] = v[j];
v[j] = aa;
}
s.clear();
s += v[0];
for(int i=1; i<v.size(); i++) {
s += " " + v[i];
}
cout << s;
}
};
int main() {
Solution s;
string str(" ");
s.reverseWords(str);
return 0;
}