Leetcode 81搜索旋转排序数组II

思路:二分法。参考http://www.cnblogs.com/grandyang/p/4325840.html。

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

你可能感兴趣的:(Leetcode 81搜索旋转排序数组II)