/*
Rotate an array of n elements to the right by k steps.
For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7]
is rotated to [5,6,7,1,2,3,4].
Note:
Try to come up as many solutions as you can, there are at least 3 different
ways to solve this problem.
*/
/*
思路:
1.整个数组翻转
2.前半部分(0-k-1)翻转
3.后半部分(k-n-1)翻转
翻转:
头尾调换实现数组翻转
*/
public class Solution {
public void rotate(int[] nums, int k) {
int len = nums.length;
// 保证k为正
k = (len + k % len) % len;
reverse(nums,0,len-1);
reverse(nums,0,k-1);
reverse(nums,k,len-1);
}
public void reverse(int[] nums,int begin,int end) {
for(;begin <= end;begin++,end--) {
int temp = nums[begin];
nums[begin] = nums[end];
nums[end] = temp;
}
}
}
leetcode-java-189. Rotate Array
最新推荐文章于 2025-10-23 14:29:22 发布
本文介绍了一种将数组元素向右旋转k步的有效算法。通过三次翻转操作实现:首先整体翻转数组,然后分别翻转前k个元素和剩余部分。这种方法简洁且高效。
416

被折叠的 条评论
为什么被折叠?



