Skip to content

Commit 0251614

Browse files
committed
151. Reverse Words in a String
1 parent 1f231b5 commit 0251614

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//https://leetcode.com/problems/reverse-words-in-a-string/
2+
3+
class Solution {
4+
public:
5+
string reverseWords(string s) {
6+
s+=" ";
7+
string temp = "";
8+
vector<string>v;
9+
10+
for(char ch: s){
11+
if(ch!=' '){
12+
temp+=ch;
13+
}
14+
else {
15+
if(temp.length())
16+
v.push_back(temp);
17+
temp = "";
18+
}
19+
}
20+
string result = "";
21+
if(v.size() && v[v.size()-1].length()) result+=v[v.size()-1];
22+
for(int i = v.size()-2; i >=0; i--)
23+
result+=" "+v[i];
24+
return result;
25+
}
26+
};

0 commit comments

Comments
 (0)