My answer in common thinking


  • 0
    J
    class Solution {
    public:
    bool search(vector<int>& nums, int target) {
        bool res=false;
        if(nums.size()==0)
            return res;
        int begin=0,end=nums.size()-1;
        while(begin<=end)
        {
            //shrink to ignore same numbers
            while(begin+1<=end&&nums[begin+1]==nums[begin])begin++;
            while(end-1>=begin&&nums[end-1]==nums[end])end--;
            // same like without duplicate
            int mid=(begin+end)/2;
            int val=nums[mid];
            if(val==target)
                return true;
            if(nums[begin]<=val)//left in order
            {
                if(nums[begin]<=target&&target<val)
                    end=mid-1;
                else
                    begin=mid+1;
            }else// right in order
            {
                if(target>val&&target<=nums[end])//in right
                {
                    begin=mid+1;
                }else
                {
                    end=mid-1;
                }
            }
        }
        return false;
    }
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.