-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution33.java
More file actions
34 lines (31 loc) · 979 Bytes
/
Solution33.java
File metadata and controls
34 lines (31 loc) · 979 Bytes
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
public class Solution33 {
public static void main(String[] args) {
int[] nums = { 4, 5, 6, 7, 0, 1, 2 };
int target = 0;
System.out.println(search(nums, target));
}
public static int search(int[] nums, int target) {
int leftLength = 0;
for (int i = 0; i < nums.length - 1; i++) {
if (nums[i] < nums[i + 1])
leftLength++;
else {
break;
}
}
int lastHalf = binarySearch(nums, leftLength + 1, nums.length - 1, target);
return lastHalf == -1 ? binarySearch(nums, 0, leftLength, target) : lastHalf;
}
public static int binarySearch(int[] nums, int left, int right, int target) {
if (left > right)
return -1;
int mid = left + (right - left) / 2;
if (nums[mid] == target)
return mid;
else if (nums[mid] > target)
return binarySearch(nums, left, mid - 1, target);
else if (nums[mid] < target)
return binarySearch(nums, mid + 1, right, target);
return -1;
}
}