LeetCode: Search in Rotated Sorted Array II 解题报告

Search in Rotated Sorted Array II

Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告":
What if duplicates are allowed?

Would this affect the run-time complexity? How and why?

Write a function to determine if a given target is in the array.

SOLUTION 1:

跟第一题类似 Search in Rotated Sorted Array

以下为回顾第一题的解析 :

和一般的二分法搜索没有太多区别。

问题是我们每次要找出正常排序的部分,你只需要比较mid, left,如果它们是正序,就代表左边是

正常排序,而右边存在断开的情况,也就是因为Rotated发生不正常序列。

例如:

4567012 如果我们取mid为7,则左边是正常序列,而右边7012不正常。

然后 我们再将target与正常排序的这边进行比较,如果target在左边,就丢弃右边,反之,丢弃

左边。一次我们可以扔掉一半。和二分搜索一样快。

=======================

第二题与第一题的区别是:

如果发现A[Mid] == A[Left]  我们不认为左边是有序的。因为有可能是这样的序列:

如 2222 34 22 | 2222 2222

如以上序列中,我们不能判断为左边有序,因为左边是存在切割点的,所以,当遇到这种情况时,

直接把Left 指针 加1,而不是丢弃一半。

对复杂度的影响:

最差复杂度为O(n),因为极端情况是所有的值相等。而有多复杂取决于有多少重复数字。假设重复

数字为m,总数为n. 则复杂度大概会是O(m + Log(n)). 因为如果我们找到一些有序序列仍然是可以扔掉一

半的。

查看代码时,请注意细微的 <= 和 < 的差别。

版本1:

 1 public boolean search1(int[] A, int target) {
 2         if (A == null || A.length == 0) {
 3             return false;
 4         }
 5
 6         int l = 0;
 7         int r = A.length - 1;
 8
 9         while (l < r - 1) {
10             int mid = l + (r - l) / 2;
11
12             if (A[mid] == target) {
13                 return true;
14             }
15
16             // left sort
17             if (A[mid] > A[l]) {
18                 // out of range.
19                 if (target > A[mid] || target < A[l]) {
20                     l = mid + 1;
21                 } else {
22                     r = mid - 1;
23                 }
24             // right sort.
25             } else if (A[mid] < A[l]) {
26                 // out of range.
27                 if (target < A[mid] || target > A[r]) {
28                     r = mid - 1;
29                 } else {
30                     l = mid + 1;
31                 }
32             } else {
33                 // move one node.
34                 l++;
35             }
36         }
37
38         if (A[l] == target || A[r] == target) {
39             return true;
40         }
41
42         return false;
43     }

版本2:

版本2仍然work的原因是,当mid靠到Left这边时,left的值与mid相同,我们这时left++就丢弃了不可用的值,所以这个算法没有问题。

LeetCode: Search in Rotated Sorted Array 解题报告- Yu‘s ... 中就不可以这样了,判断是否有序时,必须使用<=,因为题1中没有第三

个分支:直接跳过。

 1 // Version 2:
 2     public boolean search(int[] A, int target) {
 3         if (A == null || A.length == 0) {
 4             return false;
 5         }
 6
 7         int l = 0;
 8         int r = A.length - 1;
 9
10         while (l <= r) {
11             int mid = l + (r - l) / 2;
12
13             if (A[mid] == target) {
14                 return true;
15             }
16
17             // left sort
18             if (A[mid] > A[l]) {
19                 // out of range.
20                 if (target > A[mid] || target < A[l]) {
21                     l = mid + 1;
22                 } else {
23                     r = mid - 1;
24                 }
25             // right sort.
26             } else if (A[mid] < A[l]) {
27                 // out of range.
28                 if (target < A[mid] || target > A[r]) {
29                     r = mid - 1;
30                 } else {
31                     l = mid + 1;
32                 }
33             } else {
34                 // move one node.
35                 l++;
36             }
37         }
38
39         return false;
40     }

代码: GitHub代码链接

时间: 2024-12-25 17:22:33

LeetCode: Search in Rotated Sorted Array II 解题报告的相关文章

【LeetCode】Search in Rotated Sorted Array II 解题报告

[题目] Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. [解析] 相比Search in Rotated Sorted Array,在

[leetcode]Search in Rotated Sorted Array II @ Python

原题地址:https://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ 题意: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if

[LeetCode] Search in Rotated Sorted Array II [36]

题目 Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 原题链接(点我) 解题思路 这题和Search in Rotated Sorted

LeetCode——Search in Rotated Sorted Array II

Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 原题链接:https://oj.leetcode.com/problems/search

LeetCode: Search in Rotated Sorted Array II [081]

[题目] Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. [题意] 在"Search in Rotated Sorted Ar

leetcode:Search in Rotated Sorted Array II(duplicated)

题目要求: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 题目地址:https://leetcode.com/problems/sear

[leetcode]Search in Rotated Sorted Array II

问题描述: Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. "Search in Rotated Sorted Array&q

[LeetCode] Search in Rotated Sorted Array II 二分搜索

Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. Hide Tags Array Binary Search 这个是在 前一道的基础上改过来

LeetCode 新题: Find Minimum in Rotated Sorted Array II 解题报告-二分法模板解法

Find Minimum in Rotated Sorted Array II Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why?Suppose a sorted array is rotated at some pivot unknown to you be