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. Yii2.0-生成二维码实例

    原文地址:http://www.yii-china.com/post/detail/19.html

  2. 安装CentOS7重启后提示License information

    Initial setup of CentOS Linux (core) ) [x] Creat user ) [!] License information (no user will be cre ...

  3. Spring--Spring容器

    在使用Spring所提供的各种丰富而神奇的功能之前,必须要在Spring IoC容器中装配好Bean,并建立Bean和Bean之间的关联关系. Spring提供了多种配置方式来实现Bean的装配.但在 ...

  4. Python开发【第十七篇】:MySQL(一)

    一.概述 1.什么是数据库 ? 答:数据的仓库,如:在ATM的示例中我们创建了一个 db 目录,称其为数据库 2.什么是 MySQL.Oracle.SQLite.Access.MS SQL Serve ...

  5. QT入门

    QT += core gui widgets //引入需要用到的库 qDebug()<<"t="<<t<<QTime::currentTime( ...

  6. MySQL5.7 Replication主从复制配置教程

    最近配置mysql5.7主从复制的时候碰到了些问题,老老实实按老版本的步骤配置会有错误,后来自己查看了官方文档,才解决了问题,在这里总结一下5.7的配置步骤, 大体步骤跟老版本的还是一样的,只是有一些 ...

  7. Unity API

    关于 int Mathf.PingPong(t, length); 原理,相当于 #include <iostream> #include <vector> int test( ...

  8. 知识梳理HTML篇

    HTML 浏览器内核: IE:trident         Firefox : gecko        Safari/chrome : webkit        Opera : presto(新 ...

  9. AngularJS API之isXXX()

    Angular中保存了很多的判断方法,可以用来验证对象是否符合某种要求,详细的参考代码样例即可 <html> <head> <meta http-equiv=" ...

  10. ggplot2 上篇

    title: "ggplot2 上篇" author: "li_volleyball" date: "2016年4月16日" output: ...