Skip to content

Commit 684679d

Browse files
committed
二刷81
1 parent 67df264 commit 684679d

File tree

4 files changed

+105
-46
lines changed

4 files changed

+105
-46
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,82 @@
11
[#0081-search-in-rotated-sorted-array-ii]
2-
= 81. Search in Rotated Sorted Array II
2+
= 81. 搜索旋转排序数组 II
33

4-
{leetcode}/problems/search-in-rotated-sorted-array-ii/[LeetCode - Search in Rotated Sorted Array II^]
4+
https://leetcode.cn/problems/search-in-rotated-sorted-array-ii/[LeetCode - 81. 搜索旋转排序数组 II ^]
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,0,1,2,2,5,6]` might become `[2,5,6,0,0,1,2]`).
8+
在传递给函数之前,`+nums+` 在预先未知的某个下标
9+
`+k+``+0 <= k < nums.length+`)上进行了 *旋转* ,使数组变为
10+
`+[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]+`(下标
11+
*从 0 开始* 计数)。例如, `+[0,1,2,4,4,4,5,6,6,7]+` 在下标 `+5+`
12+
处经旋转后可能变为 `+[4,5,6,6,7,0,1,2,4,4]+`
913

10-
You are given a target value to search. If found in the array return `true`, otherwise return `false`.
14+
给你 *旋转后* 的数组 `+nums+` 和一个整数 `+target+`
15+
,请你编写一个函数来判断给定的目标值是否存在于数组中。如果 `+nums+`
16+
中存在这个目标值 `+target+` ,则返回 `+true+` ,否则返回 `+false+`
1117

12-
.Example 1:
18+
你必须尽可能减少整个操作步骤。
19+
20+
*示例 1:*
21+
22+
[subs="verbatim,quotes,macros"]
1323
----
14-
Input: nums = [2,5,6,0,0,1,2], target = 0
15-
Output: true
24+
输入:nums = [2,5,6,0,0,1,2], target = 0
25+
输出:true
1626
----
1727

18-
.Example 2:
28+
*示例 2:*
29+
30+
[subs="verbatim,quotes,macros"]
1931
----
20-
Input: nums = [2,5,6,0,0,1,2], target = 3
21-
Output: false
32+
输入:nums = [2,5,6,0,0,1,2], target = 3
33+
输出:false
2234
----
2335

36+
*提示:*
37+
38+
* `+1 <= nums.length <= 5000+`
39+
* `-10`^`4`^`+<= nums[i] <= 10+`^`4`^
40+
* 题目数据保证 `nums` 在预先未知的某个下标上进行了旋转
41+
* `+-10+`^`4`^`+<= target <= 10+`^`+4+`^
42+
2443
*Follow up:*
2544

2645
* This is a follow up problem to xref:0033-search-in-rotated-sorted-array.adoc[33. Search in Rotated Sorted Array], where `nums` may contain duplicates.
2746
* Would this affect the run-time complexity? How and why?
2847
48+
*进阶:*
49+
50+
* 此题与 https://leetcode-cn.com/problems/search-in-rotated-sorted-array/description/[搜索旋转排序数组] 相似,但本题中的 `nums` 可能包含 *重复* 元素。这会影响到程序的时间复杂度吗?会有怎样的影响,为什么?
51+
2952
== 解题思路
3053

3154
这道题的关键是先确定哪部分有序,然后判断目标值是否在有序区间内,如果没有则再另外一部分内。
3255

3356
另外,需要注意的就是对重复值的处理。如果左边的值和中间值相等,直接让左边下标向前移动一下,简单有效。
3457

35-
== 参考资料
36-
37-
. https://leetcode-cn.com/problems/search-in-rotated-sorted-array-ii/solution/zai-javazhong-ji-bai-liao-100de-yong-hu-by-reedfan/[搜索旋转排序数组 II - 搜索旋转排序数组 II - 力扣(LeetCode)^]
38-
39-
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
40-
41-
(i.e., `[0,0,1,2,2,5,6]` might become `[2,5,6,0,0,1,2]`).
42-
43-
You are given a target value to search. If found in the array return `true`, otherwise return `false`.
44-
45-
*Example 1:*
46-
47-
[subs="verbatim,quotes,macros"]
48-
----
49-
*Input:* nums = [2`,5,6,0,0,1,2]`, target = 0
50-
*Output:* true
5158

59+
[[src-0081]]
60+
[tabs]
61+
====
62+
一刷::
63+
+
64+
--
65+
[{java_src_attr}]
5266
----
53-
54-
*Example 2:*
55-
56-
[subs="verbatim,quotes,macros"]
57-
----
58-
*Input:* nums = [2`,5,6,0,0,1,2]`, target = 3
59-
*Output:* false
67+
include::{sourcedir}/_0081_SearchInRotatedSortedArrayII.java[tag=answer]
6068
----
69+
--
6170
62-
*Follow up:*
63-
64-
65-
* This is a follow up problem to <a href="/problems/search-in-rotated-sorted-array/description/">Search in Rotated Sorted Array</a>, where `nums` may contain duplicates.
66-
* Would this affect the run-time complexity? How and why?
67-
68-
69-
[[src-0081]]
71+
二刷::
72+
+
73+
--
7074
[{java_src_attr}]
7175
----
72-
include::{sourcedir}/_0081_SearchInRotatedSortedArrayII.java[tag=answer]
76+
include::{sourcedir}/_0081_SearchInRotatedSortedArrayIi_2.java[tag=answer]
7377
----
78+
--
79+
====
80+
81+
== 参考资料
7482

logbook/202503.adoc

+8-3
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,25 @@ endif::[]
1616
|⭕️ 递归解法非常妙!传一个参数,`next``next` 比较麻烦,传两个参数比较简单。
1717

1818
|{counter:codes2503}
19-
|{leetcode_base_url}/find-first-and-last-position-of-element-in-sorted-array/[34. Find First and Last Position of Element in Sorted Array^]
19+
|{leetcode_base_url}/find-first-and-last-position-of-element-in-sorted-array/[34. 在排序数组中查找元素的第一个和最后一个位置^]
2020
|{doc_base_url}/0034-find-first-and-last-position-of-element-in-sorted-array.adoc[题解]
2121
|✅ 思考清楚确定边界时,中间指针的移动方向即可迎刃而解。
2222

2323
|{counter:codes2503}
24-
|{leetcode_base_url}/search-in-rotated-sorted-array/[33. Search in Rotated Sorted Array^]
24+
|{leetcode_base_url}/search-in-rotated-sorted-array/[33. 搜索旋转排序数组^]
2525
|{doc_base_url}/0033-search-in-rotated-sorted-array.adoc[题解]
2626
|⭕️ 重点去处理有序部分,在有序部分内查找不到,则去另外一部分去查找。
2727

2828
|{counter:codes2503}
29-
|{leetcode_base_url}/merge-intervals/[56. Merge Intervals^]
29+
|{leetcode_base_url}/merge-intervals/[56. 合并区间^]
3030
|{doc_base_url}/0056-merge-intervals.adoc[题解]
3131
|✅ 对区间进行排序,然后快慢指针在当前数组上对其进行合并。
3232

33+
|{counter:codes2503}
34+
|{leetcode_base_url}/search-in-rotated-sorted-array-ii/[81. 搜索旋转排序数组 II^]
35+
|{doc_base_url}/0081-search-in-rotated-sorted-array-ii.adoc[题解]
36+
|✅ 关注有序区间,确定目标值在有序区间内,则在有序区间查找;反之,则在另外一部分内查找。另外,通过移动一个指针即可避开重复元素。
37+
3338
|===
3439

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

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

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ public class _0081_SearchInRotatedSortedArrayII {
1717
* Memory Usage: 38.6 MB, less than 88.73% of Java online submissions for Search in Rotated Sorted Array II.
1818
*
1919
* Copy from: https://leetcode-cn.com/problems/search-in-rotated-sorted-array-ii/solution/zai-javazhong-ji-bai-liao-100de-yong-hu-by-reedfan/[搜索旋转排序数组 II - 搜索旋转排序数组 II - 力扣(LeetCode)]
20+
*
21+
* @author D瓜哥 · https://www.diguage.com
22+
* @since 2020-02-04 22:13
2023
*/
2124
public boolean search(int[] nums, int target) {
2225
if (Objects.isNull(nums) || nums.length == 0) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0081_SearchInRotatedSortedArrayIi_2 {
4+
// tag::answer[]
5+
/**
6+
* @author D瓜哥 · https://www.diguage.com
7+
* @since 2025-03-06 14:46:31
8+
*/
9+
public boolean 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+
// 相等则直接返回
14+
if (nums[mid] == target) {
15+
return true;
16+
}
17+
if (nums[left] == nums[mid]) {
18+
// 移动左指针,即可避开重复元素
19+
left++;
20+
continue;
21+
}
22+
if (nums[left] <= nums[mid]) {
23+
//前面有序,且目标值在该区间内,则在有序部分内查找
24+
if (nums[left] <= target && target < nums[mid]) {
25+
right = mid - 1;
26+
} else {
27+
// 反之,则在无序区间内查找
28+
left = mid + 1;
29+
}
30+
} else {
31+
//后面有序,且目标值在该区间内,则在有序部分内查找
32+
if (nums[mid] < target && target <= nums[right]) {
33+
left = mid + 1;
34+
} else {
35+
// 反之,则在无序区间内查找
36+
right = mid - 1;
37+
}
38+
}
39+
}
40+
return false;
41+
}
42+
// end::answer[]
43+
}

0 commit comments

Comments
 (0)