Javascript swap beats 80%


  • 0
    R
    var wiggleSort = function(nums) {
        for(var i = 1; i < nums.length; i++) {
            if ((i % 2) ^ (nums[i] > nums[i - 1]))
                swap(nums, i);
        }
    };
    
    var swap = function(nums, i) {
        var temp = nums[i];
        nums[i] = nums[i - 1];
        nums[i - 1] =  temp;  
    };
    

Log in to reply
 

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