前言
今天的题是一道简单难度的,建议大家看完题目之后,至少想到两三种解决方法,有助于更好的掌握。
正文
原题
链接:旋转数组
Given an array, rotate the array to the right by k steps, where k is non-negative.
Example 1:
Input: [1,2,3,4,5,6,7] and k = 3
Output: [5,6,7,1,2,3,4]
Explanation:
rotate 1 steps to the right: [7,1,2,3,4,5,6]
rotate 2 steps to the right: [6,7,1,2,3,4,5]
rotate 3 steps to the right: [5,6,7,1,2,3,4]
Example 2:
Input: [-1,-100,3,99] and k = 2
Output: [3,99,-1,-100]
Explanation:
rotate 1 steps to the right: [99,-1,-100,3]
rotate 2 steps to the right: [3,99,-1,-100]
Note:
Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
Could you do it in-place with O(1) extra space?
题目大意
给定一个数组,并将数组中的元素向右移动 k 个位置。
思路1
最简单的方法就是借助一个额外的数组temp,temp存放了每一个交换过后的数,然后再将原数组nums的值改成temp的值,直接看代码吧:
代码
class Solution {
public void rotate(int[] nums, int k) {
if (nums == null || k == 0) return;
// 空间复杂度为O(N)
int[] temp = new int[nums.length];
for (int i = 0; i < nums.length; i++)
temp[(i + k) % nums.length] = nums[i];
for (int i = 0; i < nums.length; i++)
nums[i] = temp[i];
}
}
代码讲解
第一个循环中,只有一行代码,但却挺关键的,这行代码实现了位置的移动,下面的图解释了这行代码为什么这么写:
前两个还比较好理解,temp要存放数值的下标为i + k,当i = 2时,2 + 3 = 5,而下标最大的地方为4,因此temp要存放数值的下标为0,也就是说到达最后一个下标后从头开始数,那怎么用代码表示呢?
其实就是取模了,i + k之后,除以数组的长度,求得余数,比如2 + 3 = 5,5 % 5 = 0,这时候刚好就是第0个下标了,而前面的计算,我们也可以用 (i + k) % nums.length 替换。
到这里的时候,第一趟遍历就结束了,temp数组已经是正确答案了,但是由于题意要求更改nums数组,因此我们需要最后一趟遍历,更改nums的值:
for (int i = 0; i < nums.length; i++)
nums[i] = temp[i];
提交代码,时间确实有点慢。
思路2
这一次我们不使用额外的数组,我们直接在原数组进行修改。
首先模拟一下数组的移动,看图:
上面这张图只是大概描述旋转数组的过程,其实每次移动时,数组的所有元素都得跟着移动,我们仔细看一下第一次移动的过程吧:
经过了上面的移动之后,才变成了下面这张图的样子:
同理,每次都需要对数组的所有元素进行移动才会形成最终的答案:
我们可以用代码模拟这个过程,下面的代码实现了将数组右移一个单位:
// 保存最后一个数值
int temp = nums[nums.length - 1];
// 将数组右移一个单位
for (int j = nums.length - 1; j > 0; j--)
nums[j] = nums[j - 1];
// 将最后一个数放在最前面
nums[0] = temp;
总共需要移动k次,我们就在这个代码上加个for,循环k次即可,代码如下:
代码
class Solution {
public void rotate(int[] nums, int k) {
if (nums == null || k == 0) return;
// 总共需要移动k次
for (int i = 0; i < k; i++) {
int temp = nums[nums.length - 1];
for (int j = nums.length - 1; j > 0; j--)
nums[j] = nums[j - 1];
nums[0] = temp;
}
}
}
提交代码,发现时间上去了,这时的时间复杂度为O(N * k),N为数组的长度,k为移动的单位;空间复杂度为O(1)。
思路3
那可不可以将时间复杂度再改小一点呢?比如O(N),空间复杂度也是O(1),当然是可以的。
- 我们先将nums数组假设是[1,2,3,4,5] 反转得到 [5,4,3,2,1],
- 再反转0~k-1的范围假设k为3,即[0,2],得到 [3,4,5,2,1],
- 最后反转k~len-1的范围,即[3,4],得到 [3,4,5,1,2] !
是不是很神奇,当然不是我想到的,这是在LeetCode官网看到的哈哈。
代码
class Solution {
public void rotate(int[] nums, int k) {
if (nums == null || k == 0) return;
// 假设数组长度为5,则旋转3次跟旋转8次的结果是一样的
k %= nums.length;
// 先反转一整个数组
reverse(nums, 0, nums.length - 1);
// 再局部反转0~k-1范围的数组
reverse(nums, 0, k - 1);
// 最后反转k~len-1即得结果
reverse(nums, k, nums.length - 1);
}
private void reverse(int[] nums, int start, int end) {
while (start < end) {
int temp = nums[start];
nums[start++] = nums[end];
nums[end--] = temp;
}
}
}
代码讲解
为了方便,先对k进行取模,这是因为假设数组长度为5,则旋转3次跟旋转8次的结果是一样的
k %= nums.length;
先对整个数组进行反转
reverse(nums, 0, nums.length - 1);
再对局部范围的数组反转,0~k-1的范围
reverse(nums, 0, k - 1);
最后反转k~len-1即得结果
reverse(nums, k, nums.length - 1);
当然反转的方法我们需要自己写,不会很难
private void reverse(int[] nums, int start, int end) {
while (start < end) {
int temp = nums[start];
nums[start++] = nums[end];
nums[end--] = temp;
}
}
提交代码,或许这就是最优解了吧,看到这种结果就很舒服。
总结
其实还有一种类似的题型,也是剑指offer上面的,实现字符串的左旋转,思路都是一样的,感兴趣的同学可以去搜索一下。
好了就这就是今天的题了,时间过得真快,还有十几天就快要开学了,感觉自己的学习效率还是有待提高!加油!