leetcode 33 Search in Rotated Sorted Array

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;
}
}

posted @ 2017-02-15 00:01  王坤1993  阅读(142)  评论(0编辑  收藏  举报