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,而不是丢弃一半。

而这里存在一个小的数学问题:

1. 当我们发现左边是降序时,右边一定是有序的(断口一定在左边嘛)

2. 当发现左边是升序,那么肯定不会有断口,左边一定是连续有序的。

3. 当相等,无法判断,则丢弃一个元素即可。(其实改进的算法是,可以这时再判断右边是不是降序。)

对复杂度的影响:

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

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

半的。

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

版本1:

 public boolean search1(int[] A, int target) {
if (A == null || A.length == 0) {
return false;
} int l = 0;
int r = A.length - 1; while (l < r - 1) {
int mid = l + (r - l) / 2; if (A[mid] == target) {
return true;
} // left sort
if (A[mid] > A[l]) {
// out of range.
if (target > A[mid] || target < A[l]) {
l = mid + 1;
} else {
r = mid - 1;
}
// right sort.
} else if (A[mid] < A[l]) {
// out of range.
if (target < A[mid] || target > A[r]) {
r = mid - 1;
} else {
l = mid + 1;
}
} else {
// move one node.
l++;
}
} if (A[l] == target || A[r] == target) {
return true;
} return false;
}

版本2:

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

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

个分支:直接跳过。

 // Version 2:
public boolean search(int[] A, int target) {
if (A == null || A.length == 0) {
return false;
} int l = 0;
int r = A.length - 1; while (l <= r) {
int mid = l + (r - l) / 2; if (A[mid] == target) {
return true;
} // left sort
if (A[mid] > A[l]) {
// out of range.
if (target > A[mid] || target < A[l]) {
l = mid + 1;
} else {
r = mid - 1;
}
// right sort.
} else if (A[mid] < A[l]) {
// out of range.
if (target < A[mid] || target > A[r]) {
r = mid - 1;
} else {
l = mid + 1;
}
} else {
// move one node.
l++;
}
} return false;
}

SOLUTION 2:

1. 当我们发现左边是降序时,右边一定是有序的(断口一定在左边嘛)

2. 当发现左边是升序,那么肯定不会有断口,左边一定是连续有序的。

3. 当相等,无法判断,则丢弃一个元素即可。改进的算法是: 可以这时再判断右边是不是降序,如果右边是降序,则表明左边是有序

 public boolean search(int[] A, int target) {
if (A == null) {
return false;
} int l = ;
int r = A.length - ; while (l < r - ) {
int mid = l + (r - l) / ;
int value = A[mid]; if (target == value) {
return true;
} // The right side is sorted.
if (value < A[l]) {
if (target > A[r] || target < value) {
// Drop the right side.
r = mid;
} else {
// Drop the left side.
l = mid;
}
// The left side is sorted.
} else if (value > A[l]){
if (target > value || target < A[l]) {
// drop the left side.
l = mid;
} else {
r = mid;
}
} else {
if (value > A[r]) {
// The right side is unordered, so the left side should be ordered.
if (target > value || target < A[l]) {
// drop the left side.
l = mid;
} else {
r = mid;
}
} l++;
}
} if (A[l] == target) {
return true;
} else if (A[r] == target) {
return true;
} return false;
}

代码: GitHub代码链接

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

  1. 【LeetCode】81. Search in Rotated Sorted Array II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/search-in ...

  2. 【LeetCode】154. Find Minimum in Rotated Sorted Array II 解题报告(Python)

    [LeetCode]154. Find Minimum in Rotated Sorted Array II 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...

  3. [leetcode]Search in Rotated Sorted Array II @ Python

    原题地址:https://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ 题意: Follow up for "Sea ...

  4. [LeetCode] Search in Rotated Sorted Array II 在旋转有序数组中搜索之二

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

  5. LeetCode——Search in Rotated Sorted Array II

    Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...

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

    称号 Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would t ...

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

    Find Minimum in Rotated Sorted Array II Follow up for "Find Minimum in Rotated Sorted Array&quo ...

  8. [Leetcode] search in rotated sorted array ii 搜索旋转有序数组

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

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

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

随机推荐

  1. Android程序员必备精品资源

    平时写程序中不断收集到的一些比较常用的东西,分享给大家. 实用工具集锦 Android Lifecycle https://github.com/xxv/android-lifecycle TinyP ...

  2. SQL Server中临时表与表变量的区别

    我们在数据库中使用表的时候,经常会遇到两种使用表的方法,分别就是使用临时表及表变量.在实际使用的时候,我们如何灵活的在存储过程中运用它们,虽然它们实现的功能基本上是一样的,可如何在一个存储过程中有时候 ...

  3. mysql-binlog_cache_size

    二进制日志缓冲区吗,默认是32k.该参数是基于会话的,不要设置过大. 当事务的记录大于设定的binlog_cache_size时,mysql会把缓冲区中的日志信息写入一个临时文件中,所以该值也不能设置 ...

  4. How to calculate elapsed / execute time in Java

    How to calculate elapsed / execute time in JavaIn Java, you can use the following ways to measure el ...

  5. User-Agent 信息汇总(拿去就能用)

    # encoding=utf- agents = [ "Mozilla/5.0 (Linux; U; Android 2.3.6; en-us; Nexus S Build/GRK39F) ...

  6. Python Tensorflow CNN 识别验证码

    Python+Tensorflow的CNN技术快速识别验证码 文章来源于: https://www.jianshu.com/p/26ff7b9075a1 验证码处理的流程是:验证码分析和处理—— te ...

  7. mongodb 远程访问配置

    1.首先修改mongodb的配置文件 让其监听所有外网ip 编辑文件:/etc/mongodb.conf 修改后的内容如下: bind_ip = 0.0.0.0 port = 27017 auth=t ...

  8. gitlab 迁移、升级打怪之路:8.8.5--> 8.10.8 --> 8.17.8 --> 9.5.9 --> 10.1.4 --> 10.2.5

    gitlab 迁移.升级打怪之路:8.8.5--> 8.10.8 --> 8.17.8 --> 9.5.9 --> 10.1.4 --> 10.2.5 gitlab 数据 ...

  9. 香蕉派 banana pi BPI-M3 八核开源硬件开发板

     Banana PI BPI-M3 是一款8核高性能单板计算机,Banana PI BPI-M3是一款比树莓派 2 B更强悍的8核Android 5.1产品. Banana PI BPI-M3 兼 ...

  10. mui做的苹果app生成ipa后放到自己的网站上让人下载安装

    苹果的APP不通过app store的话就只能是要那个$299的企业签名证书了.这个我还不会搞,没有搞过!!! 别人已经帮忙签名好的ipa,自己再传到自己的服务器上让人下载安装,步骤如下: Hbuid ...