leetcode_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.
class Solution {
public:
bool search(int A[], int n, int target) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(A == NULL || n <) return false;
int left = ;
int right = n-;
while(left <= right){
int mid = (right - left)/ + left;
if(A[mid] == target)
return true;
if(A[mid] > A[left])
{
if(target >= A[left] && target < A[mid])
right = mid - ;
else
left = mid + ;
}else if(A[mid] < A[left]){
if(target <= A[right] && target > A[mid])
left = mid + ;
else
right = mid -;
}else{
if(A[left] == target)
return true;
++left ;
}
}
return false;
}
};
leetcode_Search in Rotated Sorted Array II的更多相关文章
- 【leetcode】Search in Rotated Sorted Array II
Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if d ...
- 49. Search in Rotated Sorted Array && Search in Rotated Sorted Array II
Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...
- Java for LeetCode 154 Find Minimum in Rotated Sorted Array II
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- [OJ] Find Minimum in Rotated Sorted Array II
LintCode 160. Find Minimum in Rotated Sorted Array II (Medium) LeetCode 154. Find Minimum in Rotated ...
- LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>
LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...
- leetcode 153. Find Minimum in Rotated Sorted Array 、154. Find Minimum in Rotated Sorted Array II 、33. Search in Rotated Sorted Array 、81. Search in Rotated Sorted Array II 、704. Binary Search
这4个题都是针对旋转的排序数组.其中153.154是在旋转的排序数组中找最小值,33.81是在旋转的排序数组中找一个固定的值.且153和33都是没有重复数值的数组,154.81都是针对各自问题的版本1 ...
- 33. Search in Rotated Sorted Array & 81. Search in Rotated Sorted Array II
33. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated at some piv ...
- LeetCode: Search in Rotated Sorted Array II 解题报告
Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告& ...
- 【LeetCode】154. Find Minimum in Rotated Sorted Array II (3 solutions)
Find Minimum in Rotated Sorted Array II Follow up for "Find Minimum in Rotated Sorted Array&quo ...
随机推荐
- LibCurl编程手册以及代码实例
1. LibCurl编程流程 在基于LibCurl的程序里,主要采用callback function (回调函数)的形式完成传输任务,用户在启动传输前设置好各类参数和回调函数,当满足条件时libcu ...
- C#的checked和unchecked
C#的 checked关键字用于对整型算术运算和转换显式启用溢出检查. 简单点说,我们在进行数值计算时,运算结果可能会超出该类型能表达的数值范围,因而结果溢出.而这个溢出如果是含有变量的表达式的话,编 ...
- Could not Build module ImageIO
错误提示:Could not Build module ImageIO: 原因:修改了XCode API 源文件 措施:从一个干净的未曾修改过的XCode中拷贝一份相应的文件添加到发生错误的XCode ...
- String path = request.getContextPath();这段什么用
<% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+ ...
- wxWidgets刚開始学习的人导引(3)——wxWidgets应用程序初体验
wxWidgets刚開始学习的人导引全文件夹 PDF版及附件下载 1 前言2 下载.安装wxWidgets3 wxWidgets应用程序初体验4 wxWidgets学习资料及利用方法指导5 用wx ...
- iOS 画音频波形曲线 根据音频数据版
效果图 DrawView.h #import <UIKit/UIKit.h> @interface DrawView : UIView @property shortshort *draw ...
- 设计模式14---设计模式之命令模式(Command)(行为型)
1.场景模拟 请用软件模拟开机过程 按下启动按钮 然后电源供电 主板开始加电自检 BIOS依次寻找其他设备的BIOS并且让他们初始化自检 开始检测CPU,内存,光盘,硬盘,光驱,串口,并口,软驱即插即 ...
- 完全卸载sql2005
转自:http://www.cnblogs.com/mytechblog/articles/1883961.html 1.Stop 所有服务 2.在控制面板中卸载所有与SQL Server 2005相 ...
- sun.misc.BASE64Encoder是内部专用 API, 可能会在未来发行版中删除
简介 MEVAN打包遇到问题“sun.misc.BASE64Encoder是内部专用 API, 可能会在未来发行版中删除”,属于警告!项目虽然能正常运行,但是有警告就是一种隐患,要将隐患消灭在萌芽中. ...
- setTimeout 理解
1.定义简介 setTimeout() 方法用于在指定的毫秒数后调用函数或计算表达式. function foo(){}; var id = setTimeout(foo,1000); //返回一 ...