81 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.
和33题的区别就是http://www.cnblogs.com/li-daphne/p/5566373.html
就是数组中的数可以是重复的。
比如
/**
1. 1.. 1... 3 4 5
5 1. 1.. 1... 2 3 4
4 5 1. 1.. 1... 2 3
3 4 5 1. 1.. 1... 2
2 3 4 1. 1.. 1...
1... 2 3 5 1. 1..
1.. 1... 2 4 5 1.
*/
1 3 1 1
1 1 1 1
1 1 3 1
1 1 1 3
3 1 1 1
图画的不好,完美的避开了所有演示。
在将nums利用mid分为左右两部分后,你不能通过nums[mid]<num[right]来判断右边是有序的,或利用nums[mid]>nums[right]判断左边是有序的。
如果nums[mid]<=nums[right]分为两部分:
如果nums[mid]<nums[right],则右边是有序,利用排除法现在有序的部分搜索;后再无序的部分搜索。
如果nums[mid]==nums[right],right--后再看下一步就行了。
-----
bool search_2(vector<int> &nums,int target){
if(nums.size()==) return false;
int left = ;
int right = nums.size()-;
while(left<=right){
int mid = (left+right)/;
if(nums[mid]==target) return true;
else if(nums[mid]<nums[right]){
//我们已经直到数组的右边是有序的,
if(nums[mid]<target && target <=nums[right])//判断有序的数组是不是存在target,方法是?
left = mid+;
else
right = mid-;
}else if(nums[mid]>nums[right]){
//这里我们假定数组左边的是有序的
if(target >=nums[left] && target<=nums[mid])///判断左边 有序的数组是不是存在target,方法是?
right = mid-;
else
left = mid+;
}else{
///nums[mid]==nums[right], right左移一位,再试一试
right--;
}
}
return false;
}
81 Search in Rotated Sorted Array II的更多相关文章
- 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】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 d ...
- [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 81. 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
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
- [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. 思路 ...
- 81. Search in Rotated Sorted Array II (Array; Divide-and-Conquer)
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
随机推荐
- mysql 操作突然断网,MySQL: “lock wait timeout exceeded”
show processlist;//显示所有进程select * from information_schema.innodb_trx;//查询锁的进程-- kill 310;//杀掉锁进程
- openjudge 大师兄,师傅被妖怪抓走啦
描述 孙悟空听到沙僧大喊一句:“大师兄,师傅被妖怪抓走啦!”于是孙悟空直追白骨精而去.孙悟空在一条长度为L的森林小路上飞奔,上面有L+1个整点,依次为0,1,2……L.白骨精会使用一种大范围的攻击法术 ...
- 骑士问题(knight) (BFS)
题目描述 在一个标准8×8的国际象棋棋盘上,棋盘中有些格子可能是有障碍物的.已知骑士的初始位置和目标位置,你的任务是计算出骑士最少需要多少步可以从初始位置到达目标位置.有障碍物的格子当然不可以到达. ...
- (转) An overview of gradient descent optimization algorithms
An overview of gradient descent optimization algorithms Table of contents: Gradient descent variants ...
- -bash: crontab: command not found(转)
操作步骤 1. 确认crontab是否安装: 执行 crontab 命令如果报 command not found,就表明没有安装 2. 安装 crontab 执行 yum install -y vi ...
- struts2 的简单实例应用
功能:本实例实现的功能是从输入界面输入用户名和密码,若用户名和密码正确转到成功界面,否则转到失败界面. 1)在创建了一个JavaWeb工程后,导入struts2的核心支持包库. 2)web项目右击-M ...
- MySQL之选择字段数据类型
MySQL支持的数据类型很多,选择正确的数据类型对于 获得高性能至关重要.在选择时有个简单的原则有助于做出更好的选择. 简单的原则: A.通常最小的是最好的 因为这样可以用更少的磁盘.内容.CPU缓存 ...
- asp.net中HTML代码编码、解码与页面的重定向
http://www.cnblogs.com/yjss/articles/2005894.html 1.编码(输出的字符串中包含HTML代码) Response.Write(Server.HtmlEn ...
- ASP.NET Cookie 概述【转】
来源:http://msdn.microsoft.com/zh-cn/library/ms178194(VS.80).aspx ASP.NET Cookie 概述 Cookie 提供了一种在 Web ...
- windows下做react native官方例子遇到的问题
1.android/app/build.gradle文件中,指定了版本: compileSdkVersion 23buildToolsVersion "23.0.1" 需要在设置中 ...