Leetcode 33. Search in Rotated Sorted Array
public class Solution{
public int search(int[] nums, int target) {
int n = nums.length;
if(n==0) return -1;
int l=0,r=n-1;
while(l<r){
if(target==nums[l]) return l;
if(target==nums[r]) return r;
if(l+1==r) return -1;
int m = (l+r)/2;
if(target>nums[l]){
if(nums[m]>=nums[l]){
if(nums[m]>=target){
r=m;
}else if(nums[m]<target){
l=m+1;
}
}else{
r=m-1;
}
}
else if(target<nums[r]){
if(nums[m]<=nums[r]){
if(nums[m]>=target){
r=m;
}else if(nums[m]<target){
l=m+1;
}
}else{
l = m+1;
}
}else{
return -1;
}
}
if(nums[l]==target) return l;
return -1;
}
}