Easy C++ code


  • 0
    G
        void reverseWords(string &s) {
            reverse(s, 0, s.size() - 1);
            int start = 0;
            for (int i = 0; i <= s.size(); i++) {
                if (s[i] == ' ' || i == s.size()) {
                    reverse(s, start, i - 1);
                    start = i + 1;
                }
            }
        }
        void reverse(string& s, int a, int b) {
            while (a < b) {
                swap(str[a++], str[b--]);
            }
        }
    

Log in to reply
 

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