Skip to content

Commit 1aee45a

Browse files
committed
三刷153
1 parent f8d4c49 commit 1aee45a

File tree

3 files changed

+88
-18
lines changed

3 files changed

+88
-18
lines changed
Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,61 @@
11
[#0153-find-minimum-in-rotated-sorted-array]
2-
= 153. Find Minimum in Rotated Sorted Array
2+
= 153. 寻找旋转排序数组中的最小值
33

4-
{leetcode}/problems/find-minimum-in-rotated-sorted-array/[LeetCode - Find Minimum in Rotated Sorted Array^]
4+
https://leetcode.cn/problems/find-minimum-in-rotated-sorted-array/[LeetCode - 153. 寻找旋转排序数组中的最小值 ^]
55

6-
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
6+
已知一个长度为 `n` 的数组,预先按照升序排列,经由 `1``n`*旋转* 后,得到输入数组。例如,原数组 `nums = [0,1,2,4,5,6,7]` 在变化后可能得到:
77

8-
(i.e., `[0,1,2,4,5,6,7]` might become `[4,5,6,7,0,1,2]`).
8+
* 若旋转 `4` 次,则可以得到 `[4,5,6,7,0,1,2]`
9+
* 若旋转 `7` 次,则可以得到 `[0,1,2,4,5,6,7]`
910
10-
Find the minimum element.
11+
注意,数组 `[a[0], a[1], a[2], ..., a[n-1]]` *旋转一次* 的结果为数组 `[a[n-1], a[0], a[1], a[2], ..., a[n-2]]`
1112

12-
You may assume no duplicate exists in the array.
13+
给你一个元素值 *互不相同* 的数组 `nums`,它原来是一个升序排列的数组,并按上述情形进行了多次旋转。请你找出并返回数组中的 *最小元素*
1314

14-
*Example 1:*
15+
你必须设计一个时间复杂度为 stem:[O(log n)] 的算法解决此问题。
1516

16-
[subs="verbatim,quotes,macros"]
17-
----
18-
*Input:* [3,4,5,1,2]
19-
*Output:* 1
20-
----
17+
*示例 1:*
18+
19+
....
20+
输入:nums = [3,4,5,1,2]
21+
输出:1
22+
解释:原数组为 [1,2,3,4,5] ,旋转 3 次得到输入数组。
23+
....
24+
25+
*示例 2:*
26+
27+
....
28+
输入:nums = [4,5,6,7,0,1,2]
29+
输出:0
30+
解释:原数组为 [0,1,2,4,5,6,7] ,旋转 3 次得到输入数组。
31+
....
32+
33+
*示例 3:*
34+
35+
....
36+
输入:nums = [11,13,15,17]
37+
输出:11
38+
解释:原数组为 [11,13,15,17] ,旋转 4 次得到输入数组。
39+
....
40+
41+
*提示:*
42+
43+
* `n == nums.length`
44+
* `+1 <= n <= 5000+`
45+
* `+-5000 <= nums[i] <= 5000+`
46+
* `nums` 中的所有整数 *互不相同*
47+
* `nums` 原来是一个升序排序的数组,并进行了 `1``n` 次旋转
2148
22-
*Example 2:*
2349
24-
[subs="verbatim,quotes,macros"]
25-
----
26-
*Input:* [4,5,6,7,0,1,2]
27-
*Output:* 0
28-
----
2950
3051
== 思路分析
3152

3253
最省事的方式当然是遍历,但是,中等难度的题目不可能一个遍历就解决了。
3354

3455
针对有序数组,首先想到的就是二分查找。但是,这里的二分查找有需要做些特别处理:要判断最低点在哪个区间。
3556

57+
只要聚焦于拥有更低点的区间即可。
58+
3659
image::images/0153-01.png[{image_attr}]
3760

3861
image::images/0153-02.png[{image_attr}]
@@ -57,8 +80,18 @@ include::{sourcedir}/_0153_FindMinimumInRotatedSortedArray.java[tag=answer]
5780
include::{sourcedir}/_0153_FindMinimumInRotatedSortedArray_2.java[tag=answer]
5881
----
5982
--
83+
84+
三刷::
85+
+
86+
--
87+
[{java_src_attr}]
88+
----
89+
include::{sourcedir}/_0153_FindMinimumInRotatedSortedArray_3.java[tag=answer]
90+
----
91+
--
6092
====
6193

94+
6295
== 参考资料
6396

6497
. https://leetcode.cn/problems/find-minimum-in-rotated-sorted-array/solutions/698479/xun-zhao-xuan-zhuan-pai-xu-shu-zu-zhong-5irwp/[153. 寻找旋转排序数组中的最小值 - 官方题解^]

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,11 @@ endif::[]
948948
|{doc_base_url}/0151-reverse-words-in-a-string.adoc[题解]
949949
|✅ 双指针,切割对调。
950950

951+
|{counter:codes2503}
952+
|{leetcode_base_url}/find-minimum-in-rotated-sorted-array/[153. 寻找旋转排序数组中的最小值^]
953+
|{doc_base_url}/0153-find-minimum-in-rotated-sorted-array.adoc[题解]
954+
|✅ 二分查找!只要聚焦于最低点所在局域即可。其他区域可以忽略。
955+
951956
|===
952957

953958
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0153_FindMinimumInRotatedSortedArray_3 {
4+
// tag::answer[]
5+
6+
/**
7+
* @author D瓜哥 · https://www.diguage.com
8+
* @since 2025-06-09 22:03:22
9+
*/
10+
public int findMin(int[] nums) {
11+
int left = 0, right = nums.length - 1;
12+
while (left < right) {
13+
int mid = left + (right - left) / 2;
14+
if (nums[0] < nums[mid] && nums[mid] < nums[nums.length - 1]) {
15+
right = mid;
16+
} else if (nums[0] < nums[mid]) {
17+
left = mid + 1;
18+
} else if (nums[mid] < nums[nums.length - 1]) {
19+
right = mid;
20+
} else {
21+
left = mid + 1;
22+
}
23+
}
24+
return nums[right];
25+
}
26+
27+
// end::answer[]
28+
public static void main(String[] args) {
29+
new _0153_FindMinimumInRotatedSortedArray_3()
30+
.findMin(new int[]{2, 1});
31+
}
32+
}

0 commit comments

Comments
 (0)