[557. Reverse Words in a String III] C++_AC


  • 0
    class Solution {
    public:
    string reverseWords(string s) {
        for(int i = 0; i < s.size();){
            while(i < s.size() && s[i] == ' ') i++;
            int j = i;
            while(j < s.size() && s[j] != ' ') j++;
            reverse(s.begin()+i, s.begin()+j );
            i = j+1;
        }
        return s;
    }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.