189. Rotate Array
来源:互联网 发布:java解压zip文件代码 编辑:程序博客网 时间:2024/05/16 10:12
Rotate Array
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.
public class Solution { public void rotate(int[] nums, int k) { if (nums.length <= 1) return; int div = nums.length - k % nums.length; reverse(nums, 0, div - 1); reverse(nums, div, nums.length - 1); reverse(nums); } public void reverse(int[] origin) { for (int i = 0; i < origin.length / 2; i++) { int temp = origin[i]; origin[i] = origin[origin.length - i - 1]; origin[origin.length - i - 1] = temp; } } public void reverse(int[] origin, int srcPos, int destPos) { while (srcPos < destPos && srcPos >= 0) { int t = origin[srcPos]; origin[srcPos] = origin[destPos]; origin[destPos] = t; srcPos++; destPos--; } }}
0 0
- Leetcode - Array - 189. Rotate Array
- 189. Rotate Array leetcode(array)
- LeetCode-Array-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
- 189. Rotate Array LeetCode
- 189. Rotate Array
- [LeetCode]189. Rotate Array
- 189. Rotate Array
- leetcode-189.-Rotate Array
- 189. Rotate Array
- 189. Rotate Array
- 189. Rotate Array
- 软件修复心得
- javascript限制只能输入数字
- iOS开发——Autolayout的实践与技巧
- ssh: connect to host slave2 port 22: Connection refused
- hadoop基础题
- 189. Rotate Array
- 记一个问题
- 【codevs1227】方格取数2,费用流
- blenderImportToUnity
- 编程之美之子数组的最大值
- Java 实验4 包与异常处理
- DOM对象和Query对象相互转换
- git Large File Storage(大文件存储)使用
- TimesTen 应用层数据库缓存学习:15. TimesTen 缓存组autorefresh的原理