Skip to content

Commit 205608e

Browse files
authored
1 parent 522f553 commit 205608e

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

searchInPivotedArrayJava

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public int search(int[] a, int target) {
3+
int low = 0, high = a.length - 1;
4+
while(low <= high) {
5+
int mid = (low + high) >> 1;
6+
if(a[mid] == target) return mid;
7+
8+
// the left side is sorted
9+
if(a[low] <= a[mid]) {
10+
if(target >= a[low] && target <= a[mid]) {
11+
high = mid - 1;
12+
}
13+
else {
14+
low = mid + 1;
15+
}
16+
}
17+
else {
18+
if(target >= a[mid] && target <= a[high]) {
19+
low = mid + 1;
20+
}
21+
else {
22+
high = mid - 1;
23+
}
24+
}
25+
}
26+
return -1;
27+
}
28+
}

0 commit comments

Comments
 (0)