Skip to content

Commit 478ad63

Browse files
committed
五刷33
1 parent c094bed commit 478ad63

File tree

3 files changed

+69
-5
lines changed

3 files changed

+69
-5
lines changed

docs/0033-search-in-rotated-sorted-array.adoc

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
[#0033-search-in-rotated-sorted-array]
22
= 33. 搜索旋转排序数组
33

4-
https://leetcode.cn/problems/search-in-rotated-sorted-array/[LeetCode - 33. 搜索旋转排序数组 ^]
4+
https://leetcode.cn/problems/search-in-rotated-sorted-array/[LeetCode - 33. 搜索旋转排序数组^]
55

6-
整数数组 `nums` 按升序排列,数组中的值 *互不相同*
6+
整数数组 `nums` 按升序排列,数组中的值 *互不相同*
77

8-
在传递给函数之前,`nums` 在预先未知的某个下标 `k``+0 <= k < nums.length+`)上进行了 *旋转*,使数组变为 `[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]`(下标 *从 0 开始* 计数)。例如, `[0,1,2,4,5,6,7]` 在下标 `3` 处经旋转后可能变为 `[4,5,6,7,0,1,2]`
8+
在传递给函数之前,`nums` 在预先未知的某个下标 `k``+0 <= k < nums.length+`)上进行了 *旋转*,使数组变为 `[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]`(下标 *从 0 开始* 计数)。例如, `[0,1,2,4,5,6,7]` 在下标 `3` 处经旋转后可能变为 `[4,5,6,7,0,1,2]`
99

10-
给你 *旋转后* 的数组 `nums` 和一个整数 `target` ,如果 `nums` 中存在这个目标值 `target` ,则返回它的下标,否则返回 `-1`
10+
给你 *旋转后* 的数组 `nums` 和一个整数 `target` ,如果 `nums` 中存在这个目标值 `target` ,则返回它的下标,否则返回 `-1`
1111

1212
你必须设计一个时间复杂度为 stem:[log_2N] 的算法解决此问题。
1313

@@ -37,7 +37,7 @@ https://leetcode.cn/problems/search-in-rotated-sorted-array/[LeetCode - 33. 搜
3737

3838
*提示:*
3939

40-
* `+1 <= nums.length <= 5000+`
40+
* `1 \<= nums.length \<= 5000`
4141
* `-10^4^ \<= nums[i] \<= 10^4^`
4242
* `nums` 中的每个值都 *独一无二*
4343
* 题目数据保证 `nums` 在预先未知的某个下标上进行了旋转
@@ -46,6 +46,10 @@ https://leetcode.cn/problems/search-in-rotated-sorted-array/[LeetCode - 33. 搜
4646
4747
== 思路分析
4848

49+
二分查找。优先在有序部分查找,在有序部分内查找不到,则去另外一部分去查找。
50+
51+
另外,`left = 0, right = length -1`,再加上整数相除,会向下取整。所以 `mid = left + (right - left) / 2` 有可能会等于 `0`,所以,*一定要使用 `nums[0] \<= nums[mid]`(注意这里的等号!)来判断前半部分是不是有序。*
52+
4953
image::images/0033-01.png[{image_attr}]
5054

5155
[[src-0033]]
@@ -86,10 +90,20 @@ include::{sourcedir}/_0033_SearchInRotatedSortedArray_3.java[tag=answer]
8690
include::{sourcedir}/_0033_SearchInRotatedSortedArray_4.java[tag=answer]
8791
----
8892
--
93+
94+
五刷::
95+
+
96+
--
97+
[{java_src_attr}]
98+
----
99+
include::{sourcedir}/_0033_SearchInRotatedSortedArray_5.java[tag=answer]
100+
----
101+
--
89102
====
90103

91104
== 参考资料
92105

93106
. https://leetcode.cn/problems/search-in-rotated-sorted-array/solutions/5906/ji-jian-solution-by-lukelee/[33. 搜索旋转排序数组 - 极简 Solution^]
107+
. https://leetcode.cn/problems/search-in-rotated-sorted-array/solutions/1987503/by-endlesscheng-auuh/[33. 搜索旋转排序数组 - 两种方法:两次二分/一次二分,简洁写法!^]
94108
. https://leetcode.cn/problems/search-in-rotated-sorted-array/solutions/220083/sou-suo-xuan-zhuan-pai-xu-shu-zu-by-leetcode-solut/[33. 搜索旋转排序数组 - 官方题解^]
95109

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2009,6 +2009,11 @@ endif::[]
20092009
|{doc_base_url}/0034-find-first-and-last-position-of-element-in-sorted-array.adoc[题解]
20102010
|✅ {doc_base_url}/0000-01-modified-binary-search.adoc[二分查找]。尝试不使用相等来确定边界。
20112011

2012+
|{counter:codes2503}
2013+
|{leetcode_base_url}/search-in-rotated-sorted-array/[33. 搜索旋转排序数组^]
2014+
|{doc_base_url}/0033-search-in-rotated-sorted-array.adoc[题解]
2015+
|⭕️ 二分查找。重点去处理有序部分,在有序部分内查找不到,则去另外一部分去查找。*一定要使用 `nums[0] \<= nums[mid]`(注意这里的等号!)来判断前半部分是不是有序。*
2016+
20122017
|===
20132018

20142019
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0033_SearchInRotatedSortedArray_5 {
4+
// tag::answer[]
5+
6+
/**
7+
* @author D瓜哥 · https://www.diguage.com
8+
* @since 2025-11-28 22:52:27
9+
*/
10+
public int search(int[] nums, int target) {
11+
int left = 0, right = nums.length - 1;
12+
int head = nums[0];
13+
int tail = nums[nums.length - 1];
14+
while (left <= right) {
15+
int mid = left + (right - left) / 2;
16+
int num = nums[mid];
17+
if (num == target) {
18+
return mid;
19+
}
20+
21+
if (head <= num) {
22+
// 前面有序,先看在不在前面
23+
if (head <= target && target < num) {
24+
right = mid - 1;
25+
} else {
26+
left = mid + 1;
27+
}
28+
} else {
29+
// 后面有序,先看在不在后面
30+
if (num < target && target <= tail) {
31+
left = mid + 1;
32+
} else {
33+
right = mid - 1;
34+
}
35+
}
36+
}
37+
return -1;
38+
}
39+
40+
// end::answer[]
41+
static void main() {
42+
new _0033_SearchInRotatedSortedArray_5()
43+
.search(new int[]{3, 1},1);
44+
}
45+
}

0 commit comments

Comments
 (0)