Skip to content

Instantly share code, notes, and snippets.

@Ulu2005
Last active March 8, 2016 22:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ulu2005/5733808a377fe7aa3e0b to your computer and use it in GitHub Desktop.
Save Ulu2005/5733808a377fe7aa3e0b to your computer and use it in GitHub Desktop.
#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