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. fork新建进程——父进程等待子进程结束

    #include <sys/types.h>#include<sys/wait.h>#include<unistd.h>#include<stdio.h> ...

  2. 让Firefox支持offsetX、offsetY

    //计算光标相对于第一个定位的父元素的坐标 function coordinate(e){ var o = window.event || e, coord, coord_X, coord_Y; co ...

  3. django 获取 POST 请求值的几种方法(转)

    转载请注明出处:http://hi.baidu.com/leejun_2005/blog/item/9a37a22238f35c5bac34de54.html from:http://stackove ...

  4. 使用本地的Nuget Repository加速Nuget访问速度

    Nuget是一个在VisualStudio下的非常好用的包管理器,然而由于众所周知的原因,其访问速度非常令人抓狂,甚至抽风.在没有VPN的环境下,如何解决这一问题呢?常见的解决方案是自己搭建一个Nug ...

  5. Qt移动应用开发(八):实现跨平台的QML和OpenGL混合渲染

    Qt移动应用开发(八):实现跨平台的QML和OpenGL混合渲染 上一篇文章讲到了利用C++这个桥梁,我们实现了QML和Java的交互.Qt 5大力推崇的QML/JS开发,让轻量.高速开发的QML/J ...

  6. JavaScript面试(-------------------------------------------)

    this是什么 方法调用模式 构造器调用模式 函数调用模式 apply/call模式 this是什么 —大多语言中,’this’代表由类实例化的当前对象.在JavaScript中,’this’通常表示 ...

  7. 【mybatis】mybatis访问报错:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found) 或者 feign被调用方使用的mybatis总报空指针异常java.lang.NullPointerException,而变量都没有问题的情况

    mybatis访问报错:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found) 需要检查的步骤: ...

  8. IOS批量截取视频截图 UIImage mp4 IOS Video

    IOS批量截取视频截图 //生成截图 NSString *path = [NSHomeDirectory() stringByAppendingString:@"/Documents&quo ...

  9. Struts2数据验证机制

    1. 手动验证的实现 只需要在继承ActionSupport类的情况下,直接重写validate()方法即可.使用validate()方法可以对用户请求的多个Action方法进行验证,但其验证的逻辑是 ...

  10. getaddrinfo详解

    #include <sys/socket.h> #include <netdb.h> int getaddrinfo(const char *restrict nodename ...