forked from haobinaa/DataStructure-DesignPattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRotate.java
55 lines (51 loc) · 1.32 KB
/
Rotate.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.haobin.leetcode.arrays;
/**
* @Author HaoBin
* @Create 2020/4/26 14:38
* @Description: 旋转数组
*
* 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数。
*
* 示例 1:
*
* 输入: [1,2,3,4,5,6,7] 和 k = 3
* 输出: [5,6,7,1,2,3,4]
* 解释:
* 向右旋转 1 步: [7,1,2,3,4,5,6]
* 向右旋转 2 步: [6,7,1,2,3,4,5]
* 向右旋转 3 步: [5,6,7,1,2,3,4]
* 示例 2:
*
* 输入: [-1,-100,3,99] 和 k = 2
* 输出: [3,99,-1,-100]
* 解释:
* 向右旋转 1 步: [99,-1,-100,3]
* 向右旋转 2 步: [3,99,-1,-100]
**/
public class Rotate {
/**
* 不开辟空间的情况下:
* 1. 倒置原数组,这样后K个元素就在前面,然后反转一下
* 2. 反转 len-k 个元素
* @param nums
* @param k
*/
public void rotate(int[] nums, int k) {
if (nums == null || nums.length == 0) {
return;
}
k = k % nums.length;
reverse(nums, 0, nums.length-1);
reverse(nums, 0, k-1);
reverse(nums, k, nums.length-1);
}
private void reverse(int[] nums, int start, int end) {
while (start < end) {
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start++;
end--;
}
}
}