public class Solution {
public int search(int[] A, int target) {
int l = 0;
int r = A.length - 1;
while (l <= r) {
int mid = (l + r) / 2;
if (target == A[mid]) return mid;
if (A[l] <= A[r]) {
if (target < A[mid]) r = mid - 1;
else l = mid + 1;
} else if (A[l] <= A[mid]) {
if (target > A[mid] || target < A[l]) l = mid + 1;
else r = mid - 1;
} else {
if (target < A[mid] || target > A[r]) r = mid - 1;
else l = mid + 1;
}
}
return -1;
}
}