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.

我的思路:

太混乱了 不提了。注意关键区分依据 排好序的一定是从小到大的

看大神的吧:

bool search(int A[], int n, int key) {
int l = , r = n - ;
while (l <= r) {
int m = l + (r - l)/;
if (A[m] == key) return true; //return m in Search in Rotated Array I
if (A[l] < A[m]) { //left half is sorted 排好序的部分一定是从小到大的
if (A[l] <= key && key < A[m]) //在排好序的这部分
r = m - ;
else
l = m + ;
} else if (A[l] > A[m]) { //right half is sorted
if (A[m] < key && key <= A[r])
l = m + ;
else
r = m - ;
} else l++; //A[l] == A[m] 把l增大1个再循环
}
return false;
}

我的代码,把三个数字都相等的情况单独处理,其他就用无重复处理。 其实我的代码看起来长一些,但是在处理1111111111115这种情况时我的方法优势还是有的

bool search(int A[], int n, int target) {
int l = , r = n - ;
while(l <= r)
{
int m = (l + r) / ;
if(target == A[m])
return true;
else if(A[l] == A[m] && A[m] == A[r]) //三个值相等
{
bool isequalleft = true;
for(int i = l; i < m; i++)
{
if(A[i] != A[i + ])
{
isequalleft = false;
break;
}
}
if(isequalleft) //去掉重复的那一半
l = m + ;
else
r = m - ;
}
else //与不重复的方法相同
{
if (A[l] <= A[m]) {
if (target >= A[l] && target < A[m]) {
r = m - ;
} else {
l = m + ;
}
} else {
if (target > A[m] && target <= A[r]) {
l = m + ;
} else {
r = m - ;
}
}
}
} return false;
}

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

  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

    Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if d ...

  3. 【leetcode】Remove Duplicates from Sorted List II (middle)

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  4. 【leetcode】Search in Rotated Sorted Array

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

  5. LeetCode 81. Search in Rotated Sorted Array II(在旋转有序序列中搜索之二)

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

  6. leetCode 81.Search in Rotated Sorted Array II (旋转数组的搜索II) 解题思路和方法

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

  7. 【题解】【数组】【查找】【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 ...

  8. 【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 ...

  9. 【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 ...

随机推荐

  1. Ubuntu 16.10 虚拟机安装记录

    一定要选自定义. 这里一定要选 稍后安装操作系统  都是坑! 启动时出现'SMBus Host Controller not enabled'错误提示,进不到图形界面. 解决办法:1.在启动Ubunt ...

  2. 让我们一起学Node.js-文章列表

    新浪的博客最近不给力,只好在博客园落个窝.至此之后,技术随笔会在博客园以及新浪的博客上同时更新,如果新浪给力的话~~~ 如果你想看先前新浪博客上分享的技术,请点击此处 忘尘子新浪博客! 我是拜读了朴灵 ...

  3. View和ViewImage设置图片

    1.view类的设置背景android:background --setBackgroundResource(int) --A drawable to use as the background. s ...

  4. HDU 3351 Seinfeld(括号匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3351 解题报告:输入一个只有'{'跟'}'的字符串,有两种操作,一种是把'{'变成'}',另一种是'} ...

  5. Android学习之路书籍推荐

    Android开发书籍推荐:从入门到精通系列学习路线书籍介绍 JAVA入门书籍: < Introduction to java programming > < Core java & ...

  6. Opencv SkinOtsu皮肤检测

    void SkinRGB(IplImage* rgb, IplImage* _dst) { assert(rgb->nChannels == && _dst->nChann ...

  7. log4j 配置

    给java项目添加log4j日志: 1.下载log4j jar包,放入lib目录, 导入项目中 2.创建log4j.properties 文件  目录 Src 3.在需要使用输出的类中使用 priva ...

  8. 38 网络相关函数(六)——live555源码阅读(四)网络

    38 网络相关函数(六)——live555源码阅读(四)网络 38 网络相关函数(六)——live555源码阅读(四)网络 简介 12)makeSocketNonBlocking和makeSocket ...

  9. jQuery常用API

    jQuery API查询网址 http://jquery.cuishifeng.cn/ Dom和jquery相互装换 jquery对象[0] => Dom对象 Dom对象 => $(Dom ...

  10. Java 重写(Overriding)和重载(Overloading)

    方法的重写(Overriding)和重载(Overloading)是java多态性的不同表现. 重写是父类与子类之间多态性的一种表现 重载是一类中多态性的一种表现.