|
1 | 1 | [#0081-search-in-rotated-sorted-array-ii]
|
2 |
| -= 81. Search in Rotated Sorted Array II |
| 2 | += 81. 搜索旋转排序数组 II |
3 | 3 |
|
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 ^] |
5 | 5 |
|
6 |
| -Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. |
| 6 | +已知存在一个按非降序排列的整数数组 `+nums+` ,数组中的值不必互不相同。 |
7 | 7 |
|
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]+` 。 |
9 | 13 |
|
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+` 。 |
11 | 17 |
|
12 |
| -.Example 1: |
| 18 | +你必须尽可能减少整个操作步骤。 |
| 19 | + |
| 20 | +*示例 1:* |
| 21 | + |
| 22 | +[subs="verbatim,quotes,macros"] |
13 | 23 | ----
|
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 |
16 | 26 | ----
|
17 | 27 |
|
18 |
| -.Example 2: |
| 28 | +*示例 2:* |
| 29 | + |
| 30 | +[subs="verbatim,quotes,macros"] |
19 | 31 | ----
|
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 |
22 | 34 | ----
|
23 | 35 |
|
| 36 | +*提示:* |
| 37 | + |
| 38 | +* `+1 <= nums.length <= 5000+` |
| 39 | +* `-10`^`4`^`+<= nums[i] <= 10+`^`4`^ |
| 40 | +* 题目数据保证 `nums` 在预先未知的某个下标上进行了旋转 |
| 41 | +* `+-10+`^`4`^`+<= target <= 10+`^`+4+`^ |
| 42 | +
|
24 | 43 | *Follow up:*
|
25 | 44 |
|
26 | 45 | * 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.
|
27 | 46 | * Would this affect the run-time complexity? How and why?
|
28 | 47 |
|
| 48 | +*进阶:* |
| 49 | + |
| 50 | +* 此题与 https://leetcode-cn.com/problems/search-in-rotated-sorted-array/description/[搜索旋转排序数组] 相似,但本题中的 `nums` 可能包含 *重复* 元素。这会影响到程序的时间复杂度吗?会有怎样的影响,为什么? |
| 51 | +
|
29 | 52 | == 解题思路
|
30 | 53 |
|
31 | 54 | 这道题的关键是先确定哪部分有序,然后判断目标值是否在有序区间内,如果没有则再另外一部分内。
|
32 | 55 |
|
33 | 56 | 另外,需要注意的就是对重复值的处理。如果左边的值和中间值相等,直接让左边下标向前移动一下,简单有效。
|
34 | 57 |
|
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 |
51 | 58 |
|
| 59 | +[[src-0081]] |
| 60 | +[tabs] |
| 61 | +==== |
| 62 | +一刷:: |
| 63 | ++ |
| 64 | +-- |
| 65 | +[{java_src_attr}] |
52 | 66 | ----
|
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] |
60 | 68 | ----
|
| 69 | +-- |
61 | 70 |
|
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 | +-- |
70 | 74 | [{java_src_attr}]
|
71 | 75 | ----
|
72 |
| -include::{sourcedir}/_0081_SearchInRotatedSortedArrayII.java[tag=answer] |
| 76 | +include::{sourcedir}/_0081_SearchInRotatedSortedArrayIi_2.java[tag=answer] |
73 | 77 | ----
|
| 78 | +-- |
| 79 | +==== |
| 80 | + |
| 81 | +== 参考资料 |
74 | 82 |
|
0 commit comments