Skip to content
New issue

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

LeetCode题解:189. 旋转数组,pop+unshift一行,JavaScript,详细注释 #114

Open
chencl1986 opened this issue Jul 30, 2020 · 0 comments

Comments

@chencl1986
Copy link
Owner

原题链接:https://leetcode-cn.com/problems/rotate-array/solution/189-xuan-zhuan-shu-zu-popunshiftyi-xing-javascript/

解题思路:

  1. 该题实际的要求就是将数组末尾的k位移动到数组前面。
  2. 循环k次,每次将数组末位pop,之后unshift到数组前方即可。
/**
 * @param {number[]} nums
 * @param {number} k
 * @return {void} Do not return anything, modify nums in-place instead.
 */
var rotate = function (nums, k) {
  for (let i = 0; i < k; i++) nums.unshift(nums.pop());
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant