-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path189.轮转数组.java
More file actions
38 lines (28 loc) · 788 Bytes
/
Copy path189.轮转数组.java
File metadata and controls
38 lines (28 loc) · 788 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
36
37
/*
* @lc app=leetcode.cn id=189 lang=java
*
* [189] 轮转数组
*/
// @lc code=start
class Solution {
public void rotate(int[] nums, int k) {
rotateReverse(nums, k);
}
// from leetcode
void rotateReverse(int[] nums, int k) {
k = k % nums.length;
reverse(nums, 0, nums.length);
reverse(nums, 0, k);
reverse(nums, k , nums.length);
}
void reverse(int[] arr, int start, int end) {
// from start(inclusive) to end(exclusive)
if (start < 0 || end > arr.length) return;
for (int i = 0; i < (end - start) / 2; i++) {
int tmp = arr[end - i - 1];
arr[end - i - 1] = arr[start + i];
arr[start + i] = tmp;
}
}
}
// @lc code=end