-
Notifications
You must be signed in to change notification settings - Fork 0
/
189.旋转数组.java
39 lines (33 loc) · 882 Bytes
/
189.旋转数组.java
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
38
/*
* @lc app=leetcode.cn id=189 lang=java
*
* [189] 旋转数组
*/
// @lc code=start
class Solution {
public void rotate(int[] nums, int k) {
// int temp,pre;
// for(int i=0;i<k;i++){
// pre = nums[nums.length-1];
// for(int j=0;j<nums.length;j++){
// temp = nums[j];
// nums[j] = pre;
// pre = temp;
// }
// }
reverse(nums, 0, nums.length-1);
reverse(nums, 0, k%nums.length-1);
reverse(nums, k%nums.length, nums.length-1);
}
//反转数组函数
public void reverse(int[] nums,int start,int end){
while(start < end){
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start++;
end--;
}
}
}
// @lc code=end