【LeetCode】81. Search in Rotated Sorted Array II (2 solutions)
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.
与Find Minimum in Rotated Sorted Array,Find Minimum in Rotated Sorted Array II,Search in Rotated Sorted Array对照看
解法一:顺序查找
class Solution {
public:
bool search(int A[], int n, int target) {
for(int i = ; i < n; i ++)
{
if(A[i] == target)
return true;
}
return false;
}
};

解法二:二分查找
关键点在于,如果mid元素与low或者high元素相同,则删除一个low或者high
class Solution {
public:
bool search(int A[], int n, int target) {
int low = ;
int high = n-;
while (low <= high)
{
int mid = (low+high)/;
if(A[mid] == target)
return true;
if (A[low] < A[mid])
{
if(A[low] <= target && target < A[mid])
//binary search in sorted A[low~mid-1]
high = mid - ;
else
//subproblem from low to high
low = mid + ;
}
else if(A[mid] < A[high])
{
if(A[mid] < target && target <= A[high])
//binary search in sorted A[mid+1~high]
low = mid + ;
else
//subproblem from low to mid-1
high = mid - ;
}
else if(A[low] == A[mid])
low += ; //A[low]==A[mid] is not the target, so remove it
else if(A[mid] == A[high])
high -= ; //A[high]==A[mid] is not the target, so remove it
}
return false;
}
};

【LeetCode】81. Search in Rotated Sorted Array II (2 solutions)的更多相关文章
- 【LeetCode】81. Search in Rotated Sorted Array II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/search-in ...
- 【Leetcode】81. Search in Rotated Sorted Array II
Question: Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? ...
- 【一天一道LeetCode】#81. Search in Rotated Sorted Array II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
- 【LeetCode】081. Search in Rotated Sorted Array II
题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would t ...
- 【LeetCode】33. Search in Rotated Sorted Array (4 solutions)
Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...
- 【leetcode】Find Minimum in Rotated Sorted Array II JAVA实现
一.题目描述 Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed ...
- 【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 【LeetCode】33. Search in Rotated Sorted Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【Leetcode】33. Search in Rotated Sorted Array
Question: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforeh ...
随机推荐
- 2015 UESTC 搜索专题N题 韩爷的梦 hash
韩爷的梦 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Descrip ...
- 阻止a标签跳转
一.在html中 1. <a href="javascript:;"></a> 2. <a href="###">&l ...
- OpenWrt包管理软件opkg的使用(极路由)
说明: 1.OpenWrt本身系统没什么问题,关键点是一些路由器尝试的限制,比如一些厂商设置成内存分区为只读,那么这个安装软件就变得没什么意义了. 2.opkg的操作有点反人类,正常步骤是查询,安装: ...
- Transistor latch improves on/off circuitry
Figure 1 shows an example of on/off circuitry commonly used in battery-operated devices. The p-chann ...
- busdog is a filter driver for MS Windows (XP and above) to sniff USB traffic.
https://code.google.com/p/busdog/ busdog is a filter driver for MS Windows (XP and above) to sniff U ...
- cocos2d-x 环境搭建
刚搬到博客园,第一次在这写博.有点小激动啊~~ 闲话不多说,这次想做一个专题,针对最近比较流行的手游开发引擎cocos2d-x,希望大家不吝赐教~ 本节主要针对环境搭建方面进行 ...
- Windows上安装Git替代cmd命令行程序
git是一个版本管理系统,利用好它,可以管理你项目文件,每个文件的更新删除修改,它都知道,还可以还原回历史的某个版本. msysgit是Windows版的Git. 1.下载地址:https://git ...
- 错误 1 error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use . See online help for details.
出现这种警告的原因是因为我们没有使用安全的字符串处理函数.如果想屏蔽这种警告,可以使用: 还可以使用其它的方法,参考: https://www.cnblogs.com/gb2013/archive/2 ...
- LaTeX排版设置图表的位置 Positioning images and tables
Positioning images and tables LATEX is an editing tool that takes care of the format so you only hav ...
- WebView利用UserAgent传递SESSIONID
mWebView.getSettings().setUserAgentString(mWebView.getSettings().getUserAgentString()+"SESSIONI ...