My still ugly java solution


  • 0
    W
    public class Solution {
    public int search(int[] nums, int target) {
        int left = 0; 
        int right = nums.length - 1;
        int mid = 0;
        while(left <= right){
            mid = (left + right)/2;
            if(nums[mid] == target){
                return mid;
            }
            if(nums[left] == target){
                return left;
            }
            if(nums[right] == target){
                return right;
            }
            if(nums[mid] < nums[right]){
                if(nums[mid] < target && nums[right] > target){
                    left = mid + 1;
                }
                else{
                    right = mid - 1;
                }
            }
            else if(nums[mid] > nums[left]){
                if(nums[mid] > target && nums[left] < target){
                    right = mid - 1;
                }
                else{
                    left = mid + 1;
                }
            }
            else{
                return -1;
            }
        }
        return -1;
    }
    

    }


Log in to reply
 

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