Skip to content

Commit bf117e9

Browse files
committed
二刷189
1 parent 6f20766 commit bf117e9

File tree

5 files changed

+102
-27
lines changed

5 files changed

+102
-27
lines changed

docs/0189-rotate-array.adoc

Lines changed: 61 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,82 @@
11
[#0189-rotate-array]
2-
= 189. Rotate Array
2+
= 189. 轮转数组
33

4-
{leetcode}/problems/rotate-array/[LeetCode - Rotate Array^]
4+
https://leetcode.cn/problems/rotate-array/[LeetCode - 189. 轮转数组 ^]
55

6-
发现官网测试用例少了一个特殊情况:如果 `k` 大于两倍数组长度,提交的测试就会报错,但是官网却显示通过
6+
给定一个整数数组 `nums`,将数组中的元素向右轮转 `k` 个位置,其中 `k` 是非负数
77

8-
数组反转的方法也不错!反转时,直接使用界限的值进行加减比较简单省事!
8+
*示例 1:*
99

10-
Given an array, rotate the array to the right by _k_ steps, where _k_ is non-negative.
10+
....
11+
输入: nums = [1,2,3,4,5,6,7], k = 3
12+
输出: [5,6,7,1,2,3,4]
13+
解释:
14+
向右轮转 1 步: [7,1,2,3,4,5,6]
15+
向右轮转 2 步: [6,7,1,2,3,4,5]
16+
向右轮转 3 步: [5,6,7,1,2,3,4]
17+
....
1118

12-
*Example 1:*
19+
*示例 2:*
1320

14-
[subs="verbatim,quotes,macros"]
15-
----
16-
*Input:* `[1,2,3,4,5,6,7]` and _k_ = 3
17-
*Output:* `[5,6,7,1,2,3,4]`
18-
*Explanation:*
19-
rotate 1 steps to the right: `[7,1,2,3,4,5,6]`
20-
rotate 2 steps to the right: `[6,7,1,2,3,4,5]
21-
`rotate 3 steps to the right: `[5,6,7,1,2,3,4]`
22-
----
21+
....
22+
输入:nums = [-1,-100,3,99], k = 2
23+
输出:[3,99,-1,-100]
24+
解释:
25+
向右轮转 1 步: [99,-1,-100,3]
26+
向右轮转 2 步: [3,99,-1,-100]
27+
....
2328

24-
*Example 2:*
29+
*提示:*
30+
31+
* `1 \<= nums.length \<= 10^5^`
32+
* `-2^31^ \<= nums[i] \<= 2^31^ - 1`
33+
* `0 \<= k \<= 10^5^`
34+
35+
36+
*进阶:*
37+
38+
* 尽可能想出更多的解决方案,至少有 *三种* 不同的方法可以解决这个问题。
39+
* 你可以使用空间复杂度为 stem:[O(1)] 的 **原地 **算法解决这个问题吗?
2540
26-
[subs="verbatim,quotes,macros"]
27-
----
28-
*Input:* `[-1,-100,3,99]` and _k_ = 2
29-
*Output:* [3,99,-1,-100]
30-
*Explanation:*
31-
rotate 1 steps to the right: [99,-1,-100,3]
32-
rotate 2 steps to the right: [3,99,-1,-100]
33-
----
3441
35-
*Note:*
42+
== 思路分析
3643
44+
注意:这里是轮转数组!如果 `k > nums.length`,那么轮转 `k % nums.length` 次和轮转 `k` 次的效果是一样的。
3745
38-
* Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
39-
* Could you do it in-place with O(1) extra space?
46+
整个过程如下:
4047
48+
AB → rev(B)rev(A) → rev(rev(B))rev(rev(A)) → BA
4149
50+
image::images/0189-10.png[{image_attr}]
4251
4352
[[src-0189]]
53+
[tabs]
54+
====
55+
一刷::
56+
+
57+
--
4458
[{java_src_attr}]
4559
----
4660
include::{sourcedir}/_0189_RotateArray.java[tag=answer]
4761
----
62+
--
63+
64+
二刷::
65+
+
66+
--
67+
[{java_src_attr}]
68+
----
69+
include::{sourcedir}/_0189_RotateArray_2.java[tag=answer]
70+
----
71+
--
72+
====
73+
74+
75+
发现官网测试用例少了一个特殊情况:如果 `k` 大于两倍数组长度,提交的测试就会报错,但是官网却显示通过。
76+
77+
数组反转的方法也不错!反转时,直接使用界限的值进行加减比较简单省事!
78+
79+
== 参考资料
4880
81+
. https://leetcode.cn/problems/rotate-array/solutions/2784427/tu-jie-yuan-di-zuo-fa-yi-tu-miao-dong-py-ryfv/[189. 轮转数组 - 原地做法,一图秒懂!附数学证明^]
82+
. https://leetcode.cn/problems/rotate-array/solutions/551039/xuan-zhuan-shu-zu-by-leetcode-solution-nipk/[189. 轮转数组 - 官方题解^] -- 环状替换有意思!

docs/images/0189-10.png

76.6 KB
Loading

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,11 @@ endif::[]
388388
|{doc_base_url}/0031-next-permutation.adoc[题解]
389389
|❌ 明白大概意思,思路不够条理,写不出代码。
390390

391+
|{counter:codes}
392+
|{leetcode_base_url}/rotate-array/[189. Rotate Array^]
393+
|{doc_base_url}/0189-rotate-array.adoc[题解]
394+
|❌ 题目理解错误,是轮转数组,不是旋转数组。
395+
391396
|===
392397

393398
截止目前,本轮练习一共完成 {codes2503} 道题。

src/main/java/com/diguage/algo/leetcode/_0189_RotateArray.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ public class _0189_RotateArray {
4646
* Memory Usage: 37.4 MB, less than 100.00% of Java online submissions for Rotate Array.
4747
*
4848
* Copy from: https://leetcode.com/problems/rotate-array/solution/[Rotate Array solution - LeetCode]
49+
*
50+
* @author D瓜哥 · https://www.diguage.com
51+
* @since 2020-01-05 13:05
4952
*/
5053
public void rotate(int[] nums, int k) {
5154
if (Objects.isNull(nums) || nums.length == 0 || k % nums.length == 0) {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0189_RotateArray_2 {
4+
// tag::answer[]
5+
6+
/**
7+
* AB → rev(B)rev(A) → rev(rev(B))rev(rev(A)) → BA
8+
*
9+
* @author D瓜哥 · https://www.diguage.com
10+
* @since 2020-01-05 13:05
11+
*/
12+
public void rotate(int[] nums, int k) {
13+
int len = nums.length;
14+
k %= len;
15+
if (k == 0) {
16+
return;
17+
}
18+
reverse(nums, 0, len - 1);
19+
reverse(nums, 0, k - 1);
20+
reverse(nums, k, len - 1);
21+
}
22+
23+
private void reverse(int[] nums, int start, int end) {
24+
while (start < end) {
25+
int temp = nums[start];
26+
nums[start] = nums[end];
27+
nums[end] = temp;
28+
start++;
29+
end--;
30+
}
31+
}
32+
// end::answer[]
33+
}

0 commit comments

Comments
 (0)