JavaScript O(n) time and O(1) space


  • 0
    var reverseWords = function(str) {
        str.reverse();
        let i = 0, j = 0;
        while (j <= str.length) {
            if (str[j] === ' ' || j === str.length) {
                for (let a = i, b = j - 1; a < b; a++, b--) {
                    [str[a], str[b]] = [str[b], str[a]];
                }
                i = j = j + 1;
            } else {
                j++;
            }
        }
    };
    

Log in to reply
 

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