s4553711
10/17/2017 - 1:50 PM

557.cpp

class Solution {
public:
    string reverseWords(string s) {
        int length = s.length();
        int i = 0;
        while(i < length && s[i] == ' ') {
            i++;
        }
        int j = i;
        while(j < length) {
            while(j < length && s[j] != ' ') {
                j++;
            }
            int low = i;
            int high = j - 1;
            while(low < high) {
                swap(s[low],s[high]);
                low++;
                high--;
            }
            while(j < length && s[j] == ' ') {
                j++;
            }
            i = j;
        }
        return s;
    }
};