LeetCode:81. Search in Rotated Sorted Array II

Posted 心释_修行

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
    }
};

 

以上是关于LeetCode:81. Search in Rotated Sorted Array II的主要内容,如果未能解决你的问题,请参考以下文章

leetcode 81. Search in Rotated Sorted Array II 搜索旋转排序数组 II(中等)

LeetCode 81. Search in Rotated Sorted Array II

LeetCode:81. Search in Rotated Sorted Array II

LeetCode-81-Search in Rotated Sorted Array II

LeetCode81. Search in Rotated Sorted Array II

[LeetCode] 81. Search in Rotated Sorted Array II