leetcode 题解:Search in Rotated Sorted Array II (旋转已排序数组查找2)
题目:
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)的更多相关文章
- [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. 思路 ...
- LeetCode OJ:Search in Rotated Sorted Array II(翻转排序数组的查找)
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- LeetCode 81 Search in Rotated Sorted Array II(循环有序数组中的查找问题)
题目链接:https://leetcode.com/problems/search-in-rotated-sorted-array-ii/#/description 姊妹篇:http://www. ...
- LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>
LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...
- [LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索 II
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- 【leetcode】Search in Rotated Sorted Array II
Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if d ...
- 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 ...
- [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. ...
- [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. ...
- LeetCode 81. Search in Rotated Sorted Array II(在旋转有序序列中搜索之二)
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
随机推荐
- windows XP系统内核文件分析(全)
Windows XP个别 System32 文件 System32 文件夹下个别要移除的文件 我们就要删除另外600 个 system32 文件...我们要一次把它们全都解决掉. 以下是我所删除的 S ...
- 缓存需要注意的问题以及使用.net正则替换字符串的方法
参考资料:http://www.infoq.com/cn/news/2015/09/cache-problems 正则替换字符串的简单方法: var regTableType = new Regex( ...
- socket的异步回调函数,采用一问一答
socket.Send(buf); AsyncCallback callback = new AsyncCallback(ReceiveData5); mysocket.BeginReceive(Wi ...
- Windows7部署WordPress傻瓜式教程(IIS7.5+MySQL+PHP+WordPress)
http://www.cnblogs.com/vengen/archive/2010/01/01/WordPressInstall.html
- Linux 基本权限(一)
1. 权限概念 root@hang:/home# ll 总用量 20#文件权限 链接数量 文件所有者 所属用户组 容量大小B 创建(修改)时间 文件名 drwxr-xr-x root root 11月 ...
- thinkphp 3+ 观后详解 (1)
最近面试了一些公司,发现自己的对于架构能力的不足,于是决定开始从最基本的代码做起.先看看大牛们怎么架构整个框架的.鉴于国外的框架比较难懂,于是就选择了国内比较流行的thinkphp来进行研究. 下面写 ...
- inline-block 前世今生
曾几何时,display:inline-block 已经深入「大街小巷」,随处可见 「display:inline-block; *display:inline; *zoom:1; 」这样的代码.如今 ...
- Nginx (基于linux)综合
重启Nginx服务:centos:测试NGINX配置文件是否有效:/usr/local/nginx/sbin/nginx -t 平滑重启:/usr/local/nginx/sbin/nginx -s ...
- Android 开发之异常处理篇(一):SDK Manager 闪退的解决方法
这个问题困扰了我很久,之前没解决,就放一放.后来我又专门拿了一个下午来找解决方法,终于搞定! 我的解决方法是修改 android.bat,直接指定java.exe所在位置,不用去调用find_java ...
- android手势事件 快速移动 长按触摸屏 按下触摸屏,并拖动
/* 用户按下触摸屏.快速移动后松开 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float vel ...