Simple Java Solution


  • 0
    S
    class Solution {
        public int search(int[] nums, int target) {
            if(nums.length==0){
                return -1;
            }else if(nums.length==1){
                if(nums[0]==target){
                    return 0;
                }else{
                    return -1;
                }
            }
            int index=-1;
            int i=0;
            while((i+1)<nums.length && nums[i]<nums[i+1]){
                i++;
            }
            int pivot=i,j=i,k=i+1;
            if(nums[j]==target){
                index=j;
                return index;
            }else{
                while(j>=0 && nums[j]!=target){
                    j--;
                }
                if(j>=0){
                    index=j;
                    return index;
                }
            }
             if(k<nums.length && nums[k]==target){
                index=k;
                return index;
            }else{
                while(k<nums.length && nums[k]!=target){
                    k++;
                }
                if(k<nums.length){
                    index=k;
                    return index;
                }
            }
            return index;
        }
    }
    

Log in to reply
 

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