Skip to content

Commit 453aaf6

Browse files
committedSep 24, 2018
Add solution 189
1 parent 8a6147e commit 453aaf6

File tree

3 files changed

+89
-1
lines changed

3 files changed

+89
-1
lines changed
 

‎README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
# LeetCode
1+
22
![LeetCode-GitHub](http://p9ucdlghd.bkt.clouddn.com/leetcode-github.png)
33

4+
<center>
5+
46
[![PRs Welcome](https://img.shields.io/badge/PRs-Welcome-brightgreen.svg)](http://makeapullrequest.com)
57
[![Language](https://img.shields.io/badge/Lang-Java%2FPython%2FJS%2FCPP%2FGo%2F...-blue.svg)](https://github.com/yanglbme/leetcode)
8+
9+
</center>
10+
11+
# <center>LeetCode Solutions</center>
12+
613
## Introduction
714
Complete solutions to Leetcode problems, updated daily.
815

@@ -19,6 +26,7 @@ Complete solutions to Leetcode problems, updated daily.
1926
| 053 | [Maximum Subarray](https://github.com/yanglbme/leetcode/tree/master/solution/053.Maximum%20Subarray) | `Array`, `Divide and Conquer`, `Dynamic Programming` |
2027
| 070 | [Climbing Stairs](https://github.com/yanglbme/leetcode/tree/master/solution/070.Climbing%20Stairs) | `Dynamic Programming` |
2128
| 083 | [Remove Duplicates from Sorted List](https://github.com/yanglbme/leetcode/tree/master/solution/083.Remove%20Duplicates%20from%20Sorted%20List) | `Linked List` |
29+
| 189 | [Rotate Array](https://github.com/yanglbme/leetcode/tree/master/solution/189.Rotate%20Array) | `Array` |
2230
| 198 | [House Robber](https://github.com/yanglbme/leetcode/tree/master/solution/198.House%20Robber) | `Dynamic Programming` |
2331
| 203 | [Remove Linked List Elements](https://github.com/yanglbme/leetcode/tree/master/solution/203.Remove%20Linked%20List%20Elements) | `Linked List` |
2432
| 237 | [Delete Node in a Linked List](https://github.com/yanglbme/leetcode/tree/master/solution/237.Delete%20Node%20in%20a%20Linked%20List) | `Linked List` |

‎solution/189.Rotate Array/README.md

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
## 旋转数组
2+
### 题目描述
3+
4+
给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数。
5+
6+
示例 1:
7+
```
8+
输入: [1,2,3,4,5,6,7] 和 k = 3
9+
输出: [5,6,7,1,2,3,4]
10+
解释:
11+
向右旋转 1 步: [7,1,2,3,4,5,6]
12+
向右旋转 2 步: [6,7,1,2,3,4,5]
13+
向右旋转 3 步: [5,6,7,1,2,3,4]
14+
```
15+
16+
示例 2:
17+
```
18+
输入: [-1,-100,3,99] 和 k = 2
19+
输出: [3,99,-1,-100]
20+
解释:
21+
向右旋转 1 步: [99,-1,-100,3]
22+
向右旋转 2 步: [3,99,-1,-100]
23+
```
24+
25+
说明:
26+
27+
- 尽可能想出更多的解决方案,至少有三种不同的方法可以解决这个问题。
28+
- 要求使用空间复杂度为 O(1) 的原地算法。
29+
30+
### 解法
31+
先对整个数组做翻转,之后分别对 [0, k - 1][k, n - 1] 的子数组进行翻转,即可得到结果。
32+
33+
```java
34+
class Solution {
35+
public void rotate(int[] nums, int k) {
36+
int n = nums.length;
37+
k %= n;
38+
if (n < 2 || k == 0) {
39+
return;
40+
}
41+
42+
rotate(nums, 0, n - 1);
43+
rotate(nums, 0, k - 1);
44+
rotate(nums, k, n - 1);
45+
}
46+
47+
private void rotate(int[] nums, int start, int end) {
48+
while (start < end) {
49+
int t = nums[start];
50+
nums[start] = nums[end];
51+
nums[end] = t;
52+
++start;
53+
--end;
54+
}
55+
}
56+
}
57+
```
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public void rotate(int[] nums, int k) {
3+
int n = nums.length;
4+
k %= n;
5+
if (n < 2 || k == 0) {
6+
return;
7+
}
8+
9+
rotate(nums, 0, n - 1);
10+
rotate(nums, 0, k - 1);
11+
rotate(nums, k, n - 1);
12+
}
13+
14+
private void rotate(int[] nums, int start, int end) {
15+
while (start < end) {
16+
int t = nums[start];
17+
nums[start] = nums[end];
18+
nums[end] = t;
19+
++start;
20+
--end;
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)
Please sign in to comment.