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. 使用CefSharp在.Net程序中嵌入Chrome浏览器(二)——参数设置

    在实现了.Net程序中嵌入Chrome浏览器后,下一步的个性化操作就是加入一些设置了,在前面的文章中,我们可以看到在使用Chrome控件前,有如下一个操作: var setting = new Cef ...

  2. linux 多线程查看工具

    参考: http://www.oschina.net/translate/command-line-tools-to-monitor-linux-performance?cmp&p=1 htt ...

  3. 第十五章 php时区报错 We selected the timezone 'UTC'

    Warning: phpinfo(): It is not safe to rely on the system's timezone settings. You are *required* to ...

  4. 第九章openwrt 703N 网口转串口+串口转网口TTL 数据传输

    原生串口      1.WR703N 自带 TTL 电平串口,设备文件为/dev/ttyATH0, 波特率 115200.但是硬件串口 没有接出来,需要自己焊线.破壳. 图 1. 正面图.两根线 TP ...

  5. FFmpeg的使用——PHP转换视频、截取视频以及JW Player播放器控制

    转载:http://blog.csdn.net/zm2714/article/details/7916440 给朋友做的一个项目中,涉及到上传视频.转换视频.自动截取已上传视频内容中的一帧做为缩略图片 ...

  6. MYSQL三个默认库的介绍

    数据库INFORMATION_SCHEMA:提供了访问数据库元数据的方式. 元数据是关于数据的数据,如数据库名或表名,列的数据类型,或访问权限等.有些时候用于表述该信息的其他术语包括“数据词典”和“系 ...

  7. C#正则表达式Regex类的介绍

    一.在C#中,要使用正则表达式类,请在源文件开头处添加以下语句: using System.Text.RegularExpressions; 二.RegEx类常用的方法 1.静态Match方法 使用静 ...

  8. VS2012利用Wix打包问题

    在用VS2012打包的时候,忽然发现没有像VS2010一样可以本地打包的项目模板,于是找了N多资料后,发现现在微软在推荐用WIX打包. 在折腾WIX打包生成界面的时候,遇到了一个很纠结的问题. Unr ...

  9. 全民Scheme(2):来自星星的你

    一门编程语言,假设不能对你思考编程的方式产生影响.就不值得去学习.--  Alan Perlis (define rember*   (lambda (a list)     (cond       ...

  10. [Functional Programming] Pointy Functor Factory

    A pointed functor is a functor with an of method class IO { // The value we take for IO is always a ...