LeetCode笔记:189. Rotate Array
来源:互联网 发布:select count,sql,统计 编辑:程序博客网 时间:2024/06/05 17:15
问题:
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].
大意:
旋转一个有n个元素的数组,右移k步。
比如,n = 7,k = 3,数组 [1,2,3,4,5,6,7] 就被旋转为 [5,6,7,1,2,3,4]。
思路:
旋转本身没太多特别好说的,我的做法是用另一个数组来记录旋转后的内容,然后复制回原数组。当然记录时是从第nums.length-k个元素开始记录,记录到末尾后再去从头开始记录到刚才那个元素。
需要注意的是这里并非返回一个数组,程序会直接读取原数组位置的内容来检查,所以需要把旋转后的结果一个个复制回去。
还有,给出的k可能会大于数组长度,这时候就要对原数组长度取模,才会得出真正需要旋转的个数。
代码(Java):
public class Solution { public void rotate(int[] nums, int k) { k %= nums.length; int[] rotateNums = new int[nums.length]; int index = 0; for (int i = nums.length-k; i < nums.length; i++) { rotateNums[index] = nums[i]; index++; } for (int i = 0; i < nums.length-k; i++) { rotateNums[index] = nums[i]; index++; } for (int i = 0; i < nums.length; i++) { nums[i] = rotateNums[i]; } return; }}
他山之石:
public void rotate(int[] nums, int k) { if(nums == null || nums.length < 2){ return; } k = k % nums.length; reverse(nums, 0, nums.length - k - 1); reverse(nums, nums.length - k, nums.length - 1); reverse(nums, 0, nums.length - 1);}private void reverse(int[] nums, int i, int j){ int tmp = 0; while(i < j){ tmp = nums[i]; nums[i] = nums[j]; nums[j] = tmp; i++; j--; }}
这里他用了三次不同的翻转函数,先翻转第一到要旋转的元素之前的元素,然后翻转要旋转的那些元素,最后翻转所有元素。这么一个过程之后就神奇地得到了正确的结果。
合集:https://github.com/Cloudox/LeetCode-Record
版权所有:http://blog.csdn.net/cloudox_
0 0
- LeetCode笔记:189. Rotate Array
- 【LeetCode笔记】Rotate Array
- [LeetCode]189.Rotate Array
- [Leetcode] 189.Rotate Array
- LeetCode 189. Rotate Array
- 【LeetCode】189.Rotate Array
- [leetcode] 189.Rotate Array
- 【leetcode】189. Rotate Array
- Leetcode 189. Rotate Array
- 189. Rotate Array LeetCode
- [LeetCode]189. Rotate Array
- leetcode-189.-Rotate Array
- LeetCode 189. Rotate Array
- leetcode 189. Rotate Array
- LeetCode *** 189. Rotate Array
- [LeetCode]189. Rotate Array
- LeetCode 189. Rotate Array
- leetcode-189. Rotate Array
- NHK Easy News 翻译 12月22日-1
- 左右fragment
- arnold渲染vr视频全景视频
- IOS- 企业App"无法升级安装应用程序 因为证书无效"的解决方案
- js 把函数作为参数传递给另一个方法
- LeetCode笔记:189. Rotate Array
- AS出现Gradle's dependency cache may be corrupt错误的解决办法
- JavaScript学习笔记 CH.2 SEC.3 字符串
- 抓交通肇事逃犯
- LINUX基础知识
- jQuery的function(a){a=a||"text";}表示的意义
- FTP服务器日志解析
- 算法——分类目录
- LeetCode473: Matchsticks to Square