-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotateArray.java
More file actions
35 lines (32 loc) · 894 Bytes
/
RotateArray.java
File metadata and controls
35 lines (32 loc) · 894 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.leetcode;
/**
* Created by jamylu on 2018/1/7.
* leetcode189.
* Rotate an array of n elements to the right by k steps.
*/
public class RotateArray {
public static void main(String[] args) {
int nums[] = {1, 2, 3, 4, 5, 6, 7};
int k = 8;
rotate(nums, k);
for (int i = 0; i < nums.length; i++) {
System.out.print(nums[i] + " ");
}
}
//将数组进行转置
public static void rotate(int[] nums, int k) {
k %= nums.length;
reverse(nums, 0, nums.length - 1);
reverse(nums, 0, k - 1);
reverse(nums, k, nums.length - 1);
}
public static void reverse(int[] nums, int start, int end) {
while (start < end) {
int tmp = nums[start];
nums[start] = nums[end];
nums[end] = tmp;
start++;
end--;
}
}
}