LeetCode:81. Search in Rotated Sorted Array II

这题是在32. Search in Rotated Sorted Array I的基础上,处理特殊情况有重复数据的问题,有两个代表例子

如3,1,1和1,1,3,1只需在原基础上考虑这两种请求,完善上一题的分类思想,还有就是二分查找的迭代会完成接

下来的事情

class Solution {
public:
    bool search(vector<int>& nums, int target) {
        int low,high;
        int media;
            
        low = 0;
        high = nums.size() - 1;
       
        
        while(low <= high){
            
            
            media = (high+low) / 2;

            if(nums[media] == target)
                return true;

            
           if(nums[media] < nums[high])
    			if(nums[media] < target && target <= nums[high])
    				low = media + 1;
    			else
    				high = media - 1;
	       else if(nums[media] > nums[high])
    			if(nums[low] <= target && target < nums[media])
    				high = media - 1;
    			else 
    				low = media + 1;
    	   else 
    	        if(nums[high] != nums[low])
    	            high = media - 1;
    	        else
                    high = high - 1;

        }
        
        return false;
    }
};

 

posted @ 2016-03-24 07:42  shijiwomen  阅读(160)  评论(0编辑  收藏  举报