Last active
March 8, 2016 22:39
-
-
Save Ulu2005/5733808a377fe7aa3e0b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <algorithm> | |
#include <vector> | |
#include <string> | |
using namespace std; | |
class Solution { | |
public: | |
void reverseWords(string &s) { | |
if (s.size() <= 1) return; | |
int i = 0; | |
while (i < s.size()) { | |
if (s[i] == ' ') { | |
i++; | |
continue; | |
} | |
int start = i; | |
while ((i < s.size()) && (s[i] != ' ')) { | |
i++; | |
} | |
int end = i - 1; | |
cout << "start " << start << " end " << end << endl; | |
while (start < end) { | |
char tmp = s[start]; | |
s[start] = s[end]; | |
s[end] = tmp; | |
start++; | |
end--; | |
} | |
} | |
reverse(s.begin(), s.end()); | |
} | |
}; | |
int main() | |
{ | |
Solution sol; | |
string input = "hello world!"; | |
sol.reverseWords(input); | |
cout << input << endl; | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment