Skip to content

Commit 20673a2

Browse files
committed
二刷33
1 parent 630d653 commit 20673a2

File tree

4 files changed

+99
-14
lines changed

4 files changed

+99
-14
lines changed
+52-14
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,77 @@
11
[#0033-search-in-rotated-sorted-array]
2-
= 33. Search in Rotated Sorted Array
2+
= 33. 搜索旋转排序数组
33

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

6-
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
6+
整数数组 `nums` 按升序排列,数组中的值 *互不相同*
77

8-
(i.e., `[0,1,2,4,5,6,7]` might become `[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-
You are given a target value to search. If found in the array return its index, otherwise return `-1`.
10+
给你 *旋转后* 的数组 `nums` 和一个整数 `target` ,如果 `nums` 中存在这个目标值 `target` ,则返回它的下标,否则返回 `-1`
1111

12-
You may assume no duplicate exists in the array.
12+
你必须设计一个时间复杂度为 stem:[log_2N] 的算法解决此问题。
1313

14-
Your algorithm's runtime complexity must be in the order of _O_(log _n_).
15-
16-
*Example 1:*
14+
*示例 1:*
1715

1816
[subs="verbatim,quotes,macros"]
1917
----
20-
*Input:* nums = [`4,5,6,7,0,1,2]`, target = 0
21-
*Output:* 4
18+
输入:nums = [4,5,6,7,0,1,2], target = 0
19+
输出:4
20+
----
2221

22+
*示例 2:*
23+
24+
[subs="verbatim,quotes,macros"]
25+
----
26+
输入:nums = [4,5,6,7,0,1,2], target = 3
27+
输出:-1
2328
----
2429

25-
*Example 2:*
30+
*示例 3:*
2631

2732
[subs="verbatim,quotes,macros"]
2833
----
29-
*Input:* nums = [`4,5,6,7,0,1,2]`, target = 3
30-
*Output:* -1
34+
输入:nums = [1], target = 0
35+
输出:-1
3136
----
3237

38+
*提示:*
39+
40+
* `+1 <= nums.length <= 5000+`
41+
* `+-10+`^`+4+`^`+ <= nums[i] <= 10+`^`+4+`^
42+
* `+nums+` 中的每个值都 *独一无二*
43+
* 题目数据保证 `+nums+` 在预先未知的某个下标上进行了旋转
44+
* `+-10+`^`+4+`^`+ <= target <= 10+`^`+4+`^
45+
46+
47+
== 思路分析
48+
49+
image::images/0033-01.png[{image_attr}]
3350

3451
[[src-0033]]
52+
[tabs]
53+
====
54+
一刷::
55+
+
56+
--
3557
[{java_src_attr}]
3658
----
3759
include::{sourcedir}/_0033_SearchInRotatedSortedArray.java[tag=answer]
3860
----
61+
--
62+
63+
二刷::
64+
+
65+
--
66+
[{java_src_attr}]
67+
----
68+
include::{sourcedir}/_0033_SearchInRotatedSortedArray_2.java[tag=answer]
69+
----
70+
--
71+
====
72+
73+
== 参考资料
74+
75+
. https://leetcode.cn/problems/search-in-rotated-sorted-array/solutions/5906/ji-jian-solution-by-lukelee/[33. 搜索旋转排序数组 - 极简 Solution^]
76+
. https://leetcode.cn/problems/search-in-rotated-sorted-array/solutions/220083/sou-suo-xuan-zhuan-pai-xu-shu-zu-by-leetcode-solut/[33. 搜索旋转排序数组 - 官方题解^]
3977

docs/images/0033-01.png

62.6 KB
Loading

logbook/202503.adoc

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ endif::[]
2020
|{doc_base_url}/0034-find-first-and-last-position-of-element-in-sorted-array.adoc[题解]
2121
|✅ 思考清楚确定边界时,中间指针的移动方向即可迎刃而解。
2222

23+
|{counter:codes2503}
24+
|{leetcode_base_url}/search-in-rotated-sorted-array/[33. Search in Rotated Sorted Array^]
25+
|{doc_base_url}/0033-search-in-rotated-sorted-array.adoc[题解]
26+
|⭕️ 重点去处理有序部分,在有序部分内查找不到,则去另外一部分去查找。
2327

2428
|===
2529

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0033_SearchInRotatedSortedArray_2 {
4+
// tag::answer[]
5+
/**
6+
* @author D瓜哥 · https://www.diguage.com
7+
* @since 2025-03-05 14:11:58
8+
*/
9+
public int search(int[] nums, int target) {
10+
int left = 0, right = nums.length - 1;
11+
while (left <= right) {
12+
int mid = left + (right - left) / 2;
13+
if (nums[mid] == target) {
14+
return mid;
15+
}
16+
if (nums[0] <= nums[mid]) {
17+
// 由于 nums[0] <= nums[mid],所以,这个分支处理的是前面有序的情况
18+
// --------------------------------------------
19+
// 上面已经判断过 nums[mid] 和 target 是否相等,
20+
// 这里就不需要再处理相等情况,所以,可以直接去 mid 左右的索引
21+
// 该分支前面有序,只需要在有序数组里去查找即可,不满足要求,则在另外一部分里。
22+
if (nums[0] <= target && target < nums[mid]) {
23+
right = mid - 1;
24+
} else {
25+
left = mid + 1;
26+
}
27+
} else {
28+
// 上面只处理前面有序的情况,那么这里就可能是后面有序的情况。
29+
// --------------------------------------------
30+
// 上面已经判断过 nums[mid] 和 target 是否相等,
31+
// 这里就不需要再处理相等情况,所以,可以直接去 mid 左右的索引
32+
// 同理,这里也只在有序的数组里去查找,不满足要求则去另外一部分查找。
33+
if (nums[mid] < target && target <= nums[nums.length - 1]) {
34+
left = mid + 1;
35+
} else {
36+
right = mid - 1;
37+
}
38+
}
39+
}
40+
return -1;
41+
}
42+
// end::answer[]
43+
}

0 commit comments

Comments
 (0)