JAVA solution


  • 0
    A

    class Solution {
    public static int search(int[] nums, int target) {
    int left=0;
    int end = nums.length-1;
    int first;
    int mid;
    while(left>=0&&end<=nums.length-1&&left<=end){
    first =nums[left];
    mid = nums[(left+end)/2];
    if(nums[end]>=nums[left]){

    			 if(mid==target){
    					return (left+end)/2;
    				}else if(mid>target){
    					end=((left+end)/2-1);
    				}else{
    					left=((left+end)/2+1);
    				}
    		 }else{
    		 if(mid>=first){
    			 if(target==mid)
    				 return (left+end)/2;
    			 if(target>mid){
    				 left=(left+end)/2+1;
    			 }else{
    				 if(target<nums[end])
    					 left = (left+end)/2+1;
    				 else if(target==nums[end])
    					 return end;
    				 else
    					 end =(left+end)/2-1;
    			 }
    		 }else{
    			 if(target==mid)
    				 return (left+end)/2;
    			 if(target>mid){
    				 if(target<nums[left])
    					 left = (left+end)/2+1;
    				 else if(target==nums[left])
    					 return left;
    				 else
    					 end =(left+end)/2-1;
    			 }else{
    				 end =(left+end)/2-1;
    			 }
    		 }
    		 }
    	}
    	return -1;
    }
    

    }


Log in to reply
 

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