We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
给定一个数组,将数组中的元素向右移动k个位置,其中k是非负数。
k
示例 1:
输入: [1,2,3,4,5,6,7] 和 k = 3 输出: [5,6,7,1,2,3,4] 解释: 向右旋转 1 步: [7,1,2,3,4,5,6] 向右旋转 2 步: [6,7,1,2,3,4,5] 向右旋转 3 步: [5,6,7,1,2,3,4]
示例 2:
输入: [-1,-100,3,99] 和 k = 2 输出: [3,99,-1,-100] 解释: 向右旋转 1 步: [99,-1,-100,3] 向右旋转 2 步: [3,99,-1,-100]
说明:
/** * @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; reverse(nums, 0, nums.length - 1); reverse(nums, 0, k - 1); reverse(nums, k, nums.length - 1); }; function reverse(nums, start, end){ while(start < end){ nums[start] = nums[start] ^ nums[end]; nums[end] = nums[start] ^ nums[end]; nums[start] = nums[start] ^ nums[end]; start++; end--; } }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
给定一个数组,将数组中的元素向右移动
k
个位置,其中k
是非负数。示例 1:
示例 2:
说明:
解答
The text was updated successfully, but these errors were encountered: