Rotate Array
2015-09-25 01:43
281 查看
题目:
Rotate an array of n elements to the right by k steps.
For example, with n = 7 and k = 3, the array
Note:
Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
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) { k = k % nums.length; int length=nums.length; reverse(nums,0,length-1); reverse(nums,0,k-1); reverse(nums,k,length-1); return; } private void reverse(int[] nums, int start, int end) { for(int i = start, j=end; i<j;i++,j--) { int temp=nums[i]; nums[i]=nums[j]; nums[j]=temp; } } }
相关文章推荐
- .net Int16 、(int Int32)、 Int64 的区别
- AlgorithmsI Exercises: Analysis of Algorithms
- 16位整数,32位整数,64位整数
- 黑马程序员--Java基础学习笔记【参数传递、访问控制符、内部类】
- Linux网络状态工具ss命令使用详解
- poj 2100 Graveyard Design (two pointers ,尺取法)
- NSIS
- 黑马程序员--Java基础学习笔记【Object类、String类】
- Nullsoft教程 NSIS初学者图文教程一
- 黑马程序员--Java基础学习笔记【数组操作、基本数据类型包装类】
- 赛后总结,赛前反思。
- inno setup教程解释脚本
- 黑马程序员--Java基础学习笔记【正则表达式、常用API】
- Inno打包教程_百度经验
- 黑马程序员--Java基础学习笔记【集合-List、泛型】
- 黑马程序员--Java基础学习笔记【集合-Set】
- 黑马程序员--Java基础学习笔记【集合-Map】
- 黑马程序员--Java基础学习笔记【异常处理】
- 黑马程序员--Java基础学习笔记【文件操作、递归】
- 黑马程序员--Java基础学习笔记【IO流-字节流、转换流】