Quite simple solution yet still best submission in C++


  • 1
    class Solution {
    public:
        int search(vector<int>& nums, int target) 
        {
            if(nums.empty()) return -1;
            int size = nums.size();
            int l = 0, r = size-1, m = 0;
            while(l < r)
            {
                m = (l+r)/2;
                if(target == nums[m]) return m;
                if(nums[m] > nums[r])
                {
                    if(nums[m]>target && nums[l]<=target) r = m;
                    else l = m+1;
                }
                else if(nums[m] < nums[r])
                {
                    if(nums[m]<target && nums[r]>=target) l = m+1;
                    else r = m;
                }
            }
            return nums[l]==target? l : -1;
        }
    };

Log in to reply
 

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