Thought:
Update the condition. This will change the Time to O(n).
Code:
public class Solution {
public boolean search(int[] A, int target) {
int start = 0;
int end = A.length - 1;
while (start <= end) {
int mid = (start + end) / 2;
if (A[mid] == target) return true;
if (A[start] > A[mid]) {
if (A[mid] <= target && target <= A[end]) start = mid + 1;
else end = mid - 1;
}else if (A[start] < A[mid]){
if (A[start] <= target && target <= A[mid]) end = mid - 1;
else start = mid + 1;
}else {
start++;
}
}
return false;
}
}
No comments:
Post a Comment