Give you an integer array nums, and an integer target.
The integer array was originally arranged in ascending order, but it was rotated at a point unknown in advance when input. (For example, the array [0,1,2,4,5,6,7] may become [4,5,6,7,0,1,2]).
Please search for target in the array. If the target value exists in the array, return its index, otherwise return -1.
Example 1:
Input: nums = [4,5,6,7,0,1,2], target = 0
Output: 4
Example 2:
Input: nums = [4,5,6,7,0,1,2 ], target = 3
Output: -1
Example 3:
Input: nums = [1], target = 0
Output: -1
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(target==nums[mid]){
return mid;
}
if(nums[mid]<nums[right]){
if(nums[mid]<target&&target<=nums[right]){
left=mid+1;
}else{
right=mid-1;
}
}
else{
if(nums[mid]>target&&target>=nums[left]){
right=mid-1;
}else{
left=mid+1;
}
}
}
return -1;
}
}