jzhangnu / Leetcode-JS-Solutions

:tropical_drink: Leetcode solutions using JavaScript.
52 stars 7 forks source link

186. Reverse Words in a String II #110

Open jzhangnu opened 7 years ago

jzhangnu commented 7 years ago
var reverseWords = function(str) {
    str.reverse();

    for(var i = 0; i < str.length; i++){
        var j = i;

        while(str[j] !== ' ' && str[j])j++;

        var jj = j - 1, ii = i;

        while(jj > ii){
            [str[ii], str[jj]] = [str[jj], str[ii]];
            ii++;
            jj--;
        }
        //while

        i = j;
    }
    //for
};