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. TCP的三次握手与四次挥手详解

    TCP的三次握手与四次挥手是TCP创建连接和关闭连接的核心流程,我们就从一个TCP结构图开始探究中的奥秘  序列号seq:占4个字节,用来标记数据段的顺序,TCP把连接中发送的所有数据字节都编上一个序 ...

  2. CPP-基础:关于多态

        类的多态特性是支持面向对象的语言最主要的特性,有过非面向对象语言开发经历的人,通常对这一章节的内容会觉得不习惯,因为很多人错误的认为,支持类的封装的语言就是支持面向对象的,其实不然,Visua ...

  3. java 一个对象多少大,占用多少内存

    1.instrumentation这种方法还是靠谱的 一个对象占用多少字节? 2.sizeof库 <!-- https://mvnrepository.com/artifact/com.carr ...

  4. Xcode 6 创建 Objective-C category

    1. Command + N 2. 选择 iOS - Source - Objective-C File 3.File Type 选择 Category,Class 填基于的类名,File填扩展的名

  5. CSS3的背景background

    CSS3中的Background属性 background: background-image || background-position/background-size || background ...

  6. ajax以及文件上传的几种方式

    方式一:通过form表单中,html input 标签的“file”完成 # 前端代码uoload.html <form method="post" action=" ...

  7. java之 单根继承与集合

    1.单根继承 概念: 单根继承,意味着所有类的继承,都继承自单一的基类的继承模式 优点: (1)所有对象都具有一个共用接口,归根到底都是相同的基本类型. (1)所有对象都具有一个共用接口,归根到底都是 ...

  8. Python9-函数-day9

    初识函数定义与调用 def my_len(): i = 0 for k in s1: i +=1 return i #返回值 # s = 'tim' s1 = '班主任阿娇' length =my_l ...

  9. shell-note-1-基础篇

    1. Shell is a program written in C. It provides an interface for users to access to the service of o ...

  10. uboot的readme

    ## (C) Copyright 2000 - 2008# Wolfgang Denk, DENX Software Engineering, wd@denx.de.## See file CREDI ...