Javascript Array reverseFromLToR(left, right)

Description

Javascript Array reverseFromLToR(left, right)


/**//  w  w  w  .  jav a2  s. c o  m
 * @param {number[]} nums
 * @param {number} k
 * @return {void} Do not return anything, modify nums in-place instead.
 */

var rotate = function (nums, k) {
  k = k % nums.length;

  nums.reverse();
  nums.reverseFromLToR(0, k - 1);
  nums.reverseFromLToR(k, nums.length - 1);
};

Array.prototype.reverseFromLToR = function (left, right) {
  if (right >= this.length) {
    return;
  }

  while (left < right) {
    var temp = this[left];
    this[left] = this[right];
    this[right] = temp;
    left++;
    right--;
  }
};

Javascript Array reverseFromLToR(left,right)


/**/*from  ww  w. ja va  2 s  .c o m*/
 * @param {number[]} nums
 * @param {number} k
 * @return {void} Do not return anything, modify nums in-place instead.
 */
 
Array.prototype.reverseFromLToR = function(left,right){
    if(right >= this.length){
        return;
    }

    while(left < right){
        var temp = this[left];
        this[left] = this[right];
        this[right] = temp;
        left++;
        right--;
    }
}
var rotate = function(nums, k) {
    k = k%nums.length;
    
    nums.reverse();
    nums.reverseFromLToR(0,k-1);
    nums.reverseFromLToR(k,nums.length-1);
};

Javascript Array reverseFromLToR(left,right)

// Leetcode #189//w  w  w . jav  a 2s  .c  o m
// Language: Javascript
// Problem: https://leetcode.com/problems/rotate-array/

/**
 * @param {number[]} nums
 * @param {number} k
 * @return {void} Do not return anything, modify nums in-place instead.
 */
 
Array.prototype.reverseFromLToR = function(left,right){
    if(right >= this.length){
        return;
    }

    while(left < right){
        var temp = this[left];
        this[left] = this[right];
        this[right] = temp;
        left++;
        right--;
    }
}
var rotate = function(nums, k) {
    k = k%nums.length;
    
    nums.reverse();
    nums.reverseFromLToR(0,k-1);
    nums.reverseFromLToR(k,nums.length-1);
};



PreviousNext

Related