LeetCode OJ--Search in Rotated Sorted Array II
http://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/
如果在数组中有重复的元素,则不一定说必定前面或者后面的一半是有序的,所以有个while循环的处理。
#include <iostream>
using namespace std; class Solution {
public:
bool binarySearch(int A[],int target,int begin,int end)
{
int mid = (begin+end)/; if(target == A[mid])
return true;
if(target == A[begin])
return true;
if(target == A[end])
return true;
if(begin>=end)
return false; while(A[begin] == A[mid])
begin++;
if(A[begin]<A[mid])//前面有序
{
if(target>A[begin] && target<A[mid]) //在前面
return binarySearch(A,target,begin,mid-);
else
return binarySearch(A,target,mid+,end); //在后面
}
else if(A[mid]<=A[end])//后面有序
{
while(A[mid] == A[end])
end--;
if(target>A[mid] && target<A[end]) //在后面
return binarySearch(A,target,mid+,end);
else
return binarySearch(A,target,begin,mid-);//在前面
}
return false;
}
bool search(int A[], int n, int target) {
return binarySearch(A,target,,n-);
}
}; int main()
{
Solution myS; int A[] = {,,,,};
bool ans = myS.search(A,,);
cout<<ans<<endl;
return ; }
LeetCode OJ--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】Search in Rotated Sorted Array II
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旋转过有序数组里找目标值II(有重)
This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates. 思路 ...
- 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 在旋转有序数组中搜索 II
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- 【LeetCode】Search in Rotated Sorted Array II(转)
原文链接 http://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ http://blog.csdn.net/linhuan ...
- 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 在旋转有序数组中搜索之二
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- 【leetcode】Search in Rotated Sorted Array II(middle)☆
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- leetCode 81.Search in Rotated Sorted Array II (旋转数组的搜索II) 解题思路和方法
Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...
随机推荐
- redux是全局状态(数据)的管理机制,局部数据没有意义
redux是全局状态(数据)的管理机制,局部数据没有意义
- WINDOWS-API:取得当前用户账户名-GetUserName
bool TFormMain::GetCurrentProcessUser(AnsiString& strUserName) { bool bRet = false; //strUserNam ...
- POI写入word docx 07 的两种方法
下载最新jar包:http://poi.apache.org/download.html 以及API 1.写入word 1.1 直接通过XWPFDocument生成 在使用XWPFDocument写d ...
- 洛谷五月月赛【LGR-047】划水记
虽然月赛有些爆炸,但我永远资瓷洛谷! 因为去接水,所以迟到了十几分钟,然后洛谷首页就打不开了-- 通过洛谷题库间接打开了比赛,看了看\(TA\),WTF?博弈论?再仔细读了读题,嗯,判断奇偶性,不过要 ...
- Python3 try-except、raise和assert解析
Python3 try-except.raise和assert解析 一.说明 关于异常捕获try-except:在学java的时候就被教育异常捕获也是java相对c的一大优点,几年下来多少也写了些代码 ...
- Bootstrap历练实例:基本按钮群组
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- vue 点击倒计时 ajax 封装
方法:function(){ var that = this; if (that.time == 0) { that.disabled = false; that.text ="点击获取&q ...
- 【php】Windows PHP及xdebug安装 安装
php version 7.0 redis 下载地址 https://pecl.php.net/package/redis 7.0版本的redis不再依赖php_igbinary.dll扩展,可以独立 ...
- 我的Python分析成长之路5
一.装饰器: 本质是函数,装饰其他函数,为其他函数添加附加功能. 原则: 1.不能修改被装饰函数的源代码. 2.不能修改被装饰函数的调用方式. 装饰器用到的知识: 1.函数即变量 (把函数体赋值给 ...
- u-boot顶层Makefile分析
1.u-boot制作命令 make forlinx_nand_ram256_config: make all; 2.顶层mkconfig分析,参考 U-BOOT顶层目录mkconfig分析 mkcon ...