-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearchInRotatedSortedArray.java
36 lines (32 loc) · 1.22 KB
/
searchInRotatedSortedArray.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
class Solution {
public int search(int[] nums, int target) {
int start = 0;
int end = nums.length - 1;
while (start <= end) {
int mid = start + (end - start) / 2;
if (nums[mid] == target) {
return mid;
}
// Check if the left half is sorted
if (nums[start] <= nums[mid]) {
if (target >= nums[start] && target < nums[mid]) {
// If target is in the left half, search in the left half
end = mid - 1;
} else {
// If target is not in the left half, search in the right half
start = mid + 1;
}
} else {
// If the left half is not sorted, the right half must be sorted
if (target > nums[mid] && target <= nums[end]) {
// If target is in the right half, search in the right half
start = mid + 1;
} else {
// If target is not in the right half, search in the left half
end = mid - 1;
}
}
}
return -1; // If target is not found
}
}