189. Rotate Array (旋转数组)
来源:互联网 发布:小学生英语软件人教版 编辑:程序博客网 时间:2024/06/05 12:47
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.
Hint:
Could you do it in-place with O(1) extra space?
Could you do it in-place with O(1) extra space?
public class Solution { public void reverse(int[] nums,int left,int right){int temp;for(int i=left;i<=(right+left)/2;i++){temp = nums[i];nums[i] = nums[right-(i-left)];nums[right-(i-left)] = temp;}} public void rotate(int[] nums, int k) { k = k % nums.length;if (nums.length < 2 || k < 1)return;reverse(nums,0,nums.length-k-1);reverse(nums,nums.length-k,nums.length-1);reverse(nums,0,nums.length-1); }}
0 0
- LeetCode 189. Rotate Array(旋转数组)
- 189. Rotate Array (旋转数组)
- 189. Rotate Array 旋转数组
- 189. Rotate Array 旋转数组
- Rotate Array数组旋转
- leetcode 189. Rotate Array-旋转数组
- leetcode 189. Rotate Array旋转数组
- LeetCode 189 Rotate Array(旋转数组)
- LeetCode 189 Rotate Array(旋转数组)
- leetcode:Rotate Array旋转数组
- Rotate Array 数组的旋转
- 【LeetCode】Rotate Array 旋转数组
- Rotate Array 数组旋转 leetcode
- leetcode Rotate Array 旋转数组
- [C++]Rotate Array 旋转数组
- Rotate Array 部分旋转数组
- LeetCode Rotate Array(数组的旋转)
- leetcode_189. Rotate Array 原地旋转数组
- 设计模式的分类和比较
- Java继承——成员访问与继承
- 存储管理
- 172. Factorial Trailing Zeroes (计算n的阶乘尾部有多少个零)
- Chip-seq流程 文献学习笔记
- 189. Rotate Array (旋转数组)
- 01-复杂度1 最大子列和问题 (20分)
- Linux 进程通信之 消息队列
- 利用Maven创建Javaweb工程时,webapp文件夹在项目根目录下没有显示怎么办?
- 欢迎使用CSDN-markdown编辑器
- PAT--1116. Come on! Let's C
- java中关于String
- 如何使用jquery刷新当前页面
- 如何使用jquery刷新当前页面