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.

Find Minimum in Rotated Sorted Array,Find Minimum in Rotated Sorted Array II,Search in Rotated Sorted Array对照看

解法一:顺序查找

class Solution {
public:
bool search(int A[], int n, int target) {
for(int i = ; i < n; i ++)
{
if(A[i] == target)
return true;
}
return false;
}
};

解法二:二分查找

关键点在于,如果mid元素与low或者high元素相同,则删除一个low或者high

class Solution {

public:
bool search(int A[], int n, int target) {
int low = ;
int high = n-;
while (low <= high)
{
int mid = (low+high)/;
if(A[mid] == target)
return true;
if (A[low] < A[mid])
{
if(A[low] <= target && target < A[mid])
//binary search in sorted A[low~mid-1]
high = mid - ;
else
//subproblem from low to high
low = mid + ;
}
else if(A[mid] < A[high])
{
if(A[mid] < target && target <= A[high])
//binary search in sorted A[mid+1~high]
low = mid + ;
else
//subproblem from low to mid-1
high = mid - ;
}
else if(A[low] == A[mid])
low += ; //A[low]==A[mid] is not the target, so remove it
else if(A[mid] == A[high])
high -= ; //A[high]==A[mid] is not the target, so remove it
}
return false;
}
};

【LeetCode】81. Search in Rotated Sorted Array II (2 solutions)的更多相关文章

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

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

  2. 【Leetcode】81. Search in Rotated Sorted Array II

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

  3. 【一天一道LeetCode】#81. Search in Rotated Sorted Array II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...

  4. 【LeetCode】081. Search in Rotated Sorted Array II

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

  5. 【LeetCode】33. Search in Rotated Sorted Array (4 solutions)

    Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...

  6. 【leetcode】Find Minimum in Rotated Sorted Array II JAVA实现

    一.题目描述 Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed ...

  7. 【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  8. 【LeetCode】33. Search in Rotated Sorted Array 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  9. 【Leetcode】33. Search in Rotated Sorted Array

    Question: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforeh ...

随机推荐

  1. Maven安装详细图文教程

    1.安装maven前需要先安装java,并设置JAVA_HOME环境变量.(详见jdk安装教程) 2.  将apache-maven-3.0.5-bin.zip放到d:\teamwork(自定义目录) ...

  2. sql 锁类型与锁机制 转

      SQL Server锁类型(SQL)收藏1. HOLDLOCK: 在该表上保持共享锁,直到整个事务结束,而不是在语句执行完立即释放所添加的锁.   2. NOLOCK:不添加共享锁和排它锁,当这个 ...

  3. Java Http 设置代理

    1.今天在Eclipse下面编译一个Http客户端时,发现可以连接局域网,连接不上外部网络,突然想起所用PC是通过代理访问网络的,设置代理后程序可以正常访问网络了: Properties props ...

  4. 关于MORMOT跨平台

    关于MORMOT跨平台 MORMOT服务端程序,支持Win32 / Win64.还有LINUX,通过FPC. 但是你能够写一个客户端在所有DELPHI支持的平台,要使用 cross-platform ...

  5. thymleaf th:text 和 th:utext 之间的区别

    1 th:text属性 可对表达式或变量求值,并将结果显示在其被包含的 html 标签体内替换原有html文本 文本连接:用“+”符号,若是变量表达式也可以用“|”符号 e.g. 若home.welc ...

  6. hdu 1398

    题目大意:输入是一个整数.输出他的拆分数(即拆分的方案数),本题与1028最大的不同之处就在于他的面额只能是整数的平方 代码如下: /* * 1398_1.cpp * * Created on: 20 ...

  7. Java中this与super

    l  对象的this引用 作用: this关键字就是让类中一个方法,访问该类中的另一个方法或属性. 1.构造器中引用该构造器正在初始化的对象. 2.在方法中引用调用该方法的对象(哪个对象调用的方法,t ...

  8. Thinkphp学习笔记3-前置和后置操作

    前置和后置操作指的是在执行某个操作方法之前和之后会自动调用的方法,不过仅对访问控制器有效. 其他的分层控制器层和内部调用控制器的情况下前置和后置操作是无效的. 系统会检测当前操作是否具有前置和后置操作 ...

  9. linux 的空命令:(冒号)

    php里面又“空操作”这个东西,于是想一想linux的命令中是否有“空命令”这种东西,搜索一下,结果发现真的有这个东西存在 -------:) 冒号 : 就是空命令.即什么也不做,是一个命令占位符 # ...

  10. Visual Studio 2015年预览设置: 辅助安装程序说明

    本文介绍了第三方应用程序安装辅助安装的 Visual Studio 2015年预览时安装的说明.如果您安装了多设备开发功能,您需要使用其他第三方软件来处理这些项目.辅助安装程序允许您将部署到您的计算机 ...