题目:

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.

说明:

1)和1比只是有重复的数字,整体仍采用二分查找

2)方法二 :

实现:

   一、我的实现:

 class Solution {
public:
bool search(int A[], int n, int target) {
if(n==||n==&&A[]!=target) return false;
if(A[]==target) return true;
int i=;
while(A[i-]<=A[i]) i++; bool pre=binary_search(A,,i,target);
bool pos=binary_search(A,i,n-i,target);
return pre==false?pos:pre;
}
private:
bool binary_search(int *B,int lo,int len,int goal)
{
int low=lo;
int high=lo+len-;
while(low<=high)
{
int middle=(low+high)/;
if(goal==B[middle])//找到,返回index
return true;
else if(B[middle]<goal)//在右边
low=middle+;
else//在左边
high=middle-;
}
return false;//没有,返回-1
}
};

二、网上开源实现:

 class Solution {
public:
bool search(int A[], int n, int target) {
int low=;
int high=n-;
while(low<=high)
{
const int middle=(low+high)/;
if(A[middle]==target) return true;
if(A[low]<A[middle])
{
if(A[low]<=target&&target<A[middle])
high=middle-;
else
low=middle+;
}
else if(A[low]>A[middle])
{
if(A[middle]<target&&target<=A[high])
low=middle+;
else
high=middle-;
}
else
low++;
}
return false;
}
};

leetcode 题解:Search in Rotated Sorted Array II (旋转已排序数组查找2)的更多相关文章

  1. [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. 思路 ...

  2. LeetCode OJ:Search in Rotated Sorted Array II(翻转排序数组的查找)

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

  3. LeetCode 81 Search in Rotated Sorted Array II(循环有序数组中的查找问题)

    题目链接:https://leetcode.com/problems/search-in-rotated-sorted-array-ii/#/description   姊妹篇:http://www. ...

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

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

  5. [LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索 II

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

  6. 【leetcode】Search in Rotated Sorted Array II

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

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

  8. [LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索之二

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

  9. [LeetCode] 33. Search in Rotated Sorted Array 在旋转有序数组中搜索

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

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

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

随机推荐

  1. python 加密解密(base64, AES)

    1. 使用base64 s1 = base64.encodestring('hello world') s2 = base64.decodestring(s1) print s1, s2 结果 1 2 ...

  2. sql表设计器的几个默认值

    sql表设计器的几个默认值: 空字符串‘’(注意是单引号) 当前时间getdate() 逻辑值0或1 汉字或英文字符串需在前面加大写N,并用单引号引起如: N'已发货'

  3. 解决ArcGIS Android Could not find class 'com.esri.android.map.MapView'问题

    环境win7 64bit sp1,eclipse 4.2.1 ,android API 16,ADT 23.0.2,arcgis android sdk 10.2.4 从arcgis-android- ...

  4. PE文件数字签名信息读取存储及格式具体解释图之上(历史代码,贴出学习)

    // 注意下图PE文件格式具体解释图中的 // IMAGE_NT_HEADERS------->OptionalHeader------>DataDirectory[IMAGE_DIREC ...

  5. wpa_supplicant 移植及 linux 命令行模式配置无线上网

    本文涉及内容为linux 命令行模式配置无线上网 及 wpa_supplicant 移植到开发板的过程,仅供参考. 1.源码下载 wpa_supplicant 源码下载地址 :http://hosta ...

  6. web自己主动保存表单

    note:当中部分源代码来源网络 所用的框架:jquery 实现的功能: 1.自己主动保存表单 2.页面刷新的时候把自己主动保存的值赋值给表单元素 思路: 1.表单值改变的时候自己主动触发函数.保存表 ...

  7. ecmall二次开发 直接实例化mysql对象

    $db = &db(); // 第一步赋值数据库类库, $db->query(sql); // 第二步执行mysql 语句; 常用的数据库函数: 得到一行数据 $user=$db-> ...

  8. android仿win8 metro磁贴布局

    代码下载     //更新代码,   这里是更新后的代码 //////////////////////// 1,含一个图片无限滚动的控件,自己实现的 2.可新增删除每个磁贴 3.来个图片吧 ////* ...

  9. range与xrange之间的差异(转)

    range 函数说明:range([start,] stop[, step]),根据start与stop指定的范围以及step设定的步长,生成一个序列.range示例: >>> ra ...

  10. 分享8款绚丽的HTML5/jQuery特效插件

    有几天没有分享前端资源了,今天要向大家分享15款非常给力的HTML5/jQuery特效插件,废话少说,一起来看看. 1.CSS3图片重力感应特效 很酷的一款CSS3模拟重力感应特效,你可以拖动图片来甩 ...