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 ...
随机推荐
- MPMoviePlayerController 视频播放器—IOS开发
MPMoviePlayerController 与AVAudioPlayer有点类似,前者播放视频,后者播放音频,不过也有很大不同,MPMoviePlayerController 可以直接通过远程UR ...
- js ajax post提交 ie和火狐、谷歌提交的编码不一致,导致中文乱码
今天遇到一个问题找了很久发现: 使用js ajax post提交 ie和火狐.谷歌提交的编码不一致,导致中文乱码 //http://www.cnblogs.com/QGC88 $.ajax({ url ...
- 解决sqlserver使用IP无法连接的问题,用localhost或者‘“.”可以连接
今天装了一个mssql发现用ip无法连接但是用localhost和“.”却可以连接,纠结了一天终于找到了问题的解决办法: 打开mssql配置管理器(我点电脑---->右键选择管理--->服 ...
- JS、jqueryie6浏览器下使用js无法提交表单的解决办法
-----------------------JS.jqueryie6浏览器下使用js无法提交表单的解决办法---------------------------------------------- ...
- JedisPool连接池实现难点
[http://jiangwenfeng762.iteye.com/blog/1280700] [可改进的问题] 问题是jedispool有没有办法监控状态,比如说当前连接有多少,当前idle连接 ...
- WSO2 ESB
什么是WSO2 ESB? WSO2 ESB是一个轻量级的易于使用的企业服务资源总线.WSO2 ESB允许系统管理员和SOA架构师,消息路由,虚拟化,中介,转换,日志记录,任务调度,负载平衡,失败了路由 ...
- Windows 错误代码
Error Messages for Windows http://www.gregorybraun.com/MSWINERR.ZIP Server 4.0 Error Messages Code ...
- WinForm特效:拦截窗体上各个部位的点击
windows窗体的标题栏无法直接通过一些默认的事件来控制,需要了解和WM_NCHITTEST相关的windows消息. 以下示例演示了最简单的效果片断: 他会把客户区和标题栏的效果互换,比如无法按住 ...
- C++ Code_TabControl
主题 1. 选项卡控件基础 2. 显示图标的选项卡 3. 选项卡控件高级 4. 5. 属性 选项卡控件基础 1.插入1个对话框,新建1个类 CCDialog1,1 个对话框对应一个 ...
- Android-Chat-Widget
Original: https://github.com/ijarobot/Android-Chat-Widget Backup: https://github.com/eltld/Android-C ...