forked from hongtaocai/code_interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearchinrotatedsortedarray.java
More file actions
executable file
·33 lines (33 loc) · 975 Bytes
/
searchinrotatedsortedarray.java
File metadata and controls
executable file
·33 lines (33 loc) · 975 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
public class Solution {
public int search(int[] A, int target) {
// Start typing your Java solution below
// DO NOT write main() function
int left = 0;
int right = A.length-1;
while(left<=right){
int mid = left +(right-left)/2;
if(target==A[mid]) return mid;
if(A[left]<=A[mid] && A[mid]<=A[right]){
if(target>A[mid]){
left = mid+1;
}else{
right = mid-1;
}
}
else if(A[left]<=A[mid]){
if(target>=A[left] && target<A[mid]){
right = mid-1;
}else{
left = mid+1;
}
}else{
if(target>A[mid] && target<=A[right]){
left = mid+1;
}else{
right = mid-1;
}
}
}
return -1;
}
}