Java neat code


  • 0
    public boolean search(int[] nums, int target) {
      int s = 0, e = nums.length-1;
      
      while(s < e){
        int m = (s + e)/2;
        if(nums[m] == target) return true;
        
        if(nums[s] < nums[m]){
          if(nums[s] <= target && target < nums[m]){
            e = m;
          } else {
            s = m + 1;
          }
        } else if(nums[m] < nums[e]){
          if(nums[m] < target && target <= nums[e]){
            s = m + 1;
          } else {
            e = m;
          }
        } else {
            if(nums[s] == target) return true;
            s++;
        }
      }
      
      return nums[s] == target;
    }

Log in to reply
 

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