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

Leetcode Search in Rotated Sorted Array II

时间:2014-07-03 21:59:51      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   cti   for   io   

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.

class Solution {
public:
    bool search(int A[], int n, int target) {
        int left = 0, right = n-1;
        while(left <= right){
            int mid = left+(right-left)/2;
            if(A[mid] == target) return true;
            if(A[left]< A[mid]){
                if(A[left] <=target && A[mid] > target) right = mid-1;
                else left  = mid+1;
            }else if(A[left] > A[mid]){
                if(A[mid] < target && target <= A[right]) left = mid+1;
                else right =mid-1;
            }else left++;
        }
        return false;
    }
};

 

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

Leetcode Search in Rotated Sorted Array II

标签:style   blog   color   cti   for   io   

原文地址:http://www.cnblogs.com/xiongqiangcs/p/3820966.html

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