码迷,mamicode.com
首页 > 其他好文 > 详细

Search in Rotated Sorted Array II

时间:2014-06-04 20:17:05      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   java   

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.

bubuko.com,布布扣
class Solution {
public:
    bool search(int A[], int n, int target) {
        if(n==0return false;
        
        return binary(A,0,n-1,target);
    }
    bool binary(int A[],int x1,int x2,int target)
    {
        if(x1>x2) return false;
        
        int m=(x1+x2)/2;
        if(A[m]==target) return true;
        if(A[x1]<A[x2])
        {
            if(A[m]<target) return binary(A,m+1,x2,target);
            else return binary(A,x1,m-1,target);
        }
        else
        {
            return binary(A,x1,m-1,target) || binary(A,m+1,x2,target);
        }
    }
}; 
bubuko.com,布布扣

Search in Rotated Sorted Array II,布布扣,bubuko.com

Search in Rotated Sorted Array II

标签:c   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/erictanghu/p/3759515.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!