3ms C simple solution


  • 0
    A
    int search(int* nums, int numsSize, int target) {
           int left, right, mid;
           left = 0;
           right = numsSize-1;
      
           if (right<left)
               return -1;
           while ( left<=right) {
           mid = (left+right)/2;
     
          if (target == nums[mid])
              return mid;
          if(nums[mid]>=nums[left]){
              if(target >= nums[left] && target < nums[mid])
                  right = mid-1;
              else
                  left = mid+1;
          }
          else {
                  if(target <= nums[right] && target > nums[mid]) {
                      left = mid + 1;
                  } else
                      right = mid-1;
                  }
          }
          
          return -1;
      }
        
        
    

Log in to reply
 

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