-
Notifications
You must be signed in to change notification settings - Fork 0
/
33.java
43 lines (42 loc) · 1.08 KB
/
33.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Solution {
public int search(int[] nums, int target) {
int n1 = searchMin(nums,target);
Arrays.sort(nums);
int l = searchNum(nums,target);
if(l==nums.length||nums[l]!=target){
return -1;
}
return (n1+l)%nums.length;
}
private int searchNum(int[] nums,int target){
int left = 0;
int right = nums.length-1;
while(left<=right){
int mid = left + (right-left)/2;
if(nums[mid]<target){
left = mid+1;
}
else{
right = mid-1;
}
}
return left;
}
private int searchMin(int[] nums,int target){
int left = 0;
int right = nums.length-2;
while(left<=right){
int mid = left + (right-left)/2;
if(nums[mid]<nums[nums.length-1]){
right = mid-1;
}
else{
left = mid+1;
}
}
if(nums[nums.length-1]<nums[left]){
return nums.length-1;
}
return left;
}
}