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的更多相关文章

  1. LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>

    LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...

  2. 【leetcode】Search in Rotated Sorted Array II

    Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if d ...

  3. [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. 思路 ...

  4. 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 ...

  5. [LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索 II

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

  6. 【LeetCode】Search in Rotated Sorted Array II(转)

    原文链接 http://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ http://blog.csdn.net/linhuan ...

  7. LeetCode 81. Search in Rotated Sorted Array II(在旋转有序序列中搜索之二)

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

  8. [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. ...

  9. 【leetcode】Search in Rotated Sorted Array II(middle)☆

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

  10. leetCode 81.Search in Rotated Sorted Array II (旋转数组的搜索II) 解题思路和方法

    Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...

随机推荐

  1. django连接Oracle过程中出现的问题

    开始时版本信息: python 3.6   +   ce_oracle 6 最终版本信息: python 3.5   + ce_oracle 5.2 ce_oracle版本问题 cx_Oracle-5 ...

  2. webpack 使用总结

    参考:http://www.ferecord.com/webpack-summary.html#base64 写的比较详细了

  3. 系统学习爬虫_2_urllib

    什么是urllib urlopen urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=None, cad ...

  4. Shell脚本调用SQL文格式

    Shell脚本调用SQL文格式 1. 定义需要执行的SQL文,以及需要输出文件 OUTFILE=\${DATADIR}/\${FILENAME} SQLFILE=\${DATADIR}/check_t ...

  5. awk纯干货

    AWK的惊人表现: Awk设计的目的:简化一般文本处理的工作. 属于POSIX的一部分. AWK命令行: Awk的调用可以定义变量.提供程序并且指定输入文件: Awk [ -F fs ]  [ -v ...

  6. docker资源汇总

    https://github.com/hangyan/docker-resources/blob/master/README_zh.md   https://github.com/lightning- ...

  7. (33)zabbix proxy分布式监控配置

    概述 zabbix proxy可以代替zabbix server检索客户端的数据,然后把数据汇报给zabbix server,并且在一定程度上分担了zabbix server的压力.zabbix pr ...

  8. Linux基础学习-使用PXE+Kickstart无人值守安装服务

    无人值守安装系统 PXE(Preboot eXecute Environment,预启动执行环境)是由Intel公司开发的技术,可以让计算机通过网络来启动操作系统(前提是计算机上安装的网卡支持PXE技 ...

  9. js-DOM-css的className相关

    1.在非标准的浏览器,IE8及以下的浏览器不支持className的操作,包括getElementByClassName,addClassName,removeClassName;  2.getEle ...

  10. fsm三种建模思路比较

    ==================================================================================================== ...