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 affect the run-time complexity? How and why?
Write a function to determine if a given target is in the array.
思路:此时可能存在nums[start]=nums[end]或者nums[start]=nums[mid]或者nums[mid]=nums[end]。所以无法用原来方法判断是否正序、右侧rotate、或者左侧rotate。解决方法是,当碰到nums[start]=nums[end]的情况时,end-1,寻找不同元素再进行二分法。
class Solution {
public:
bool search(vector<int>& nums, int target) {
return binarySearch(nums,,nums.size()-, target);
} bool binarySearch(vector<int>& nums, int start, int end, int target){
if(start==end){
if(nums[start]==target) return true;
else return false;
} if(nums[start]==nums[end]) return binarySearch(nums,start,end-,target); //ignore duplicate int mid = start+ ((end-start)>>);
//正序
if(nums[mid]>=nums[start] && nums[mid]<nums[end]){ //mid可能=start,所以>=
if(target <= nums[mid]) return binarySearch(nums,start,mid,target); //mid肯定<end,所以至少舍弃了一个
else return binarySearch(nums,mid+,end,target); //mid+1,至少舍弃了一个
} //右侧rotate
else if(nums[mid]>=nums[start] && nums[mid]>=nums[end]){
if(target>=nums[start] && target<=nums[mid]) return binarySearch(nums,start,mid,target);
else return binarySearch(nums,mid+,end,target);
} //左侧rotate
else{
if(target>=nums[start] || target<=nums[mid]) return binarySearch(nums,start,mid,target);
else return binarySearch(nums,mid+,end,target);
}
}
};
81. Search in Rotated Sorted Array II (Array; Divide-and-Conquer)的更多相关文章
- 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. ...
- 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(在旋转有序序列中搜索之二)
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. 思路 ...
随机推荐
- django管理后台添加admin账号
直接用命令行添加即可:python manage.py createsuperuser # python manage.py createsuperuser Username (leave blank ...
- 浅谈 foreach 的原理
package com.shenzhou; import java.util.ArrayList; import java.util.Iterator; import java.util.List; ...
- PHP中Notice: unserialize(): Error at offset of bytes in on line 的解决方法
使用unserialize函数将数据储存到数据库的时候遇到了这个报错,后来发现是将gb2312转换成utf-8格式之后,每个中文的字节数从2个增加到3个之后导致了反序列化的时候判断字符长度出现了问题, ...
- Django 数据库的迁移
先数据库迁移的两大命令: python manage.py makemigrations & python manage.py migrate 前者是将model层转为迁移文件migratio ...
- React Native,flexbox布局
Flexbox布局 flex:使组件在可利用的空间内动态地扩张或收缩.flex:1会使组件撑满空间.当有多个组件都指定了flex的值,那么谁的flex值大谁占得空间就大,占得大小的比例就是flex值的 ...
- Linux后门入侵检测工具
一.rootkit简介 rootkit是Linux平台下最常见的一种木马后门工具,它主要通过替换系统文件来达到入侵和和隐蔽的目的,这种木马比普通木马后门更加危险和隐蔽,普通的检测工具和检查手段很难发现 ...
- window 服务
c#写windows服务 序言 前段时间做一个数据迁移项目,刚开始用B/S架构做的项目,但B/S要寄存在IIs中,而IIs又不稳定因素,如果重启IIs就要打开页面才能运行项目.有不便之处,就改用W ...
- CentOS7下搭建LNMP记录
OS:使用虚拟机最小化安装的CentOS 7系统:配置好防火墙,SELINUX,YUM源,IP地址等. 0.因为采用了最小化安装系统,所缺少的包比较多,必要工具先安装一下 yum install gc ...
- Jetty-attack-test
import httplib, urllib, ssl, string, sys, getopt from urlparse import urlparse ''' Author: Gotham Di ...
- Swift中的的注释
1. Swift支持与OC中相同的 /**/ ./***/ 不同点Swift支持 /*/**/ 这样的注释 ,多行注释 2. 直接双杠注释 // 单行注释 3. 利用 //MARK: 返 ...