Just a thought, no need to convert a string to array, then reverse...


  • 0
    C
    var reverseStr = function(s, k) {
        var result = [];
        for (var i = 0; i * k <= s.length; i++) {
            if (i & 1) {
                for (var j = i * k; j < (i + 1) * k; j++) result.push(s.charAt(j));
            } else {
                for (var m = (i + 1) * k - 1; m >= i * k; m--) result.push(s.charAt(m));
            }
        }
        return result.join('');
    };
    

Log in to reply
 

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