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.

 
与I类似,只是在二分搜索前,只是在判断左右两边是否有序时更加麻烦一些。
此时复杂度是O(n)了,和采用线性搜索的方法基本一致
 
 class Solution {
public:
bool search(int A[], int n, int target) { int left=;
int right=n-;
int mid;
while(left<=right)
{ mid=(left+right)/;
if(A[mid]==target)
{
return true;
} //增加了这些判断语句
bool isLeft=true; for(int i=left;i<=mid;i++)
{
if(A[i]>A[mid])
{
isLeft=false;
break;
}
} if(isLeft)
{
if(A[left]<=target&&target<A[mid])
{
right=mid-;
}
else
{
left=mid+;
}
}
else
{
if(A[mid]<target&&target<=A[right])
{
left=mid+;
}
else
{
right=mid-;
}
} }
return false; }
};

【leetcode】Search in Rotated Sorted Array II的更多相关文章

  1. 【LeetCode】Search in Rotated Sorted Array II(转)

    原文链接 http://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ http://blog.csdn.net/linhuan ...

  2. 【leetcode】Search in Rotated Sorted Array II(middle)☆

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

  3. 【leetcode】Search in Rotated Sorted Array

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

  4. 【题解】【数组】【查找】【Leetcode】Search in Rotated Sorted Array

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  5. 【LeetCode】Search in Rotated Sorted Array——旋转有序数列找目标值

    [题目] Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 ...

  6. 【leetcode】Search in Rotated Sorted Array (hard)

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  7. LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>

    LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...

  8. [leetcode]81. Search in Rotated Sorted Array II旋转过有序数组里找目标值II(有重)

    This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates. 思路 ...

  9. Java for LeetCode 081 Search in Rotated Sorted Array II

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

随机推荐

  1. 单选框的回显c:if

    <input type="radio" name="sex" value="boy" <c:if test="${te ...

  2. Mongodb3.0.6副本集+分片学习笔记

    一.使用问题记录 1. mongodb3.0.6使用mongostat参数 >./mongostat -h 127.0.0.1:27017 -u root -p 123456 /authenti ...

  3. 总结一下安装linux系统经验-版本选择-安装ubuntu

    linux版本选择: 初次接触,建议选 Ubuntu 或者 Fedora,这两个发行版都很容易上手,而且两者都有很强大的中文社区,遇到问题比较容易解决,而且都有国内的源,安装或者更新软件时体验相对会好 ...

  4. [Asp.Net]获取客户端ip和mac地址

    摘要 有时候,我们需要获取客户端的一些信息,以便进行统计.比如:客户端的唯一标识,ip等信息 IP 通过获取HTTP_X_FORWARDED_FOR,或者REMOTE_ADDR可以获取客户端的ip. ...

  5. Swing杂记——Swing中引入Android的NinePatch技术,让Swing拥有Android的外观定制能力

    [摘要] 本文诣在展示如何在Swing中引入 NinePatch技术(早期有文章里中文译作九格图,暂且这么叫吧^_^,但此术非传统移动手机上的功能布局——九格图哦). [准备篇] Q:何为 NineP ...

  6. Linux下,telnet命令如何退出

    测试连接本地的memcached telnet 链接后是这样的: wangkongming@Vostro ~ $ telnet Trying 127.0.0.1... Connected to 127 ...

  7. jupyter notebook + pyspark 环境搭建

    安装并启动jupyter 安装 Anaconda 后, 再安装 jupyter pip install jupyter 设置环境 ipython --ipython-dir= # override t ...

  8. Nginx安装配置(转)

    Nginx 安装配置 Nginx("engine x")是一款是由俄罗斯的程序设计师Igor Sysoev所开发高性能的 Web和 反向代理 服务器,也是一个 IMAP/POP3/ ...

  9. C#深入浅出 关键字(一)

    1.this this关键字用于指示当前对象“自己”,来看一个例子,了解什么时候需要用this class Star { String name; int age; public void SetIn ...

  10. ajax基础了解

    使用Ajax的最大优点,就是能在不更新整个页面的前提下维护数据.这使得Web应用程序更为迅捷地回应用户动作,并避免了在网络上发送那些没有改变过的信息.AJAX即“Asynchronous JavaSc ...