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
189. 轮转数组
提示 给定一个整数数组 nums,将数组中的元素向右轮转 k 个位置,其中 k 是非负数。
示例 1:
输入: nums = [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:
输入:nums = [-1,-100,3,99], k = 2 输出:[3,99,-1,-100] 解释: 向右轮转 1 步: [99,-1,-100,3] 向右轮转 2 步: [3,99,-1,-100]
提示:
The text was updated successfully, but these errors were encountered:
/** Do not return anything, modify nums in-place instead. */ function rotate(nums: number[], k: number): void { const n = nums.length; const newArr = new Array(n); for (let i = 0; i < n; ++i) { newArr[(i + k) % n] = nums[i]; } for (let i = 0; i < n; ++i) { nums[i] = newArr[i]; } };
Sorry, something went wrong.
/** Do not return anything, modify nums in-place instead. */ function rotate(nums: number[], k: number): void { const arr = [], n = nums.length; for (let i = 0; i < n; i++) { arr[(i + k) % n] = nums[i]; } for (let i = 0; i < n; i++) { nums[i] = arr[i]; } };
No branches or pull requests
189. 轮转数组
提示
给定一个整数数组 nums,将数组中的元素向右轮转 k 个位置,其中 k 是非负数。
示例 1:
示例 2:
提示:
The text was updated successfully, but these errors were encountered: