Leetcode 189. Rotate Array
来源:互联网 发布:管理车间 软件系统 编辑:程序博客网 时间:2024/04/30 02:43
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]
.
The idea is from http://www.programcreek.com/2015/03/rotate-array-in-java/, The trick is to reverse the array
public static void rotate(int[] arr, int order) {order = order % arr.length; if (arr == null || order < 0) {throw new IllegalArgumentException("Illegal argument!");} //length of first partint a = arr.length - order; reverse(arr, 0, a-1);reverse(arr, a, arr.length-1);reverse(arr, 0, arr.length-1); } public static void reverse(int[] arr, int left, int right){if(arr == null || arr.length == 1) return; while(left < right){int temp = arr[left];arr[left] = arr[right];arr[right] = temp;left++;right--;}}
0 0
- [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
- Leetcode 189. Rotate Array
- leetcode 189. Rotate Array
- mysql中用!= 或者<>注意
- Generate java file from XSD, missing @XMLRootElement annotation.
- Leetcode 81. Search in Rotated Sorted Array II
- Leetcode 74. Search a 2D Matrix
- CF R340
- Leetcode 189. Rotate Array
- Leetcode 238. Product of Array Except Self
- Leetcode 128. Longest Consecutive Sequence
- vxWorks启动顺
- 2016.1.23___Java and UML surgeory
- 【自用】 memset对于int、long long、float、double 的极值怎么清
- vim中代码注释与取消的两种方法
- 1415: [Noi2005]聪聪和可可 概率与期望 记忆化搜索
- BZOJ 3809: Gty的二逼妹子序列|莫队算法|分块