2017.9.25
哇,完全不知道这三道搜索区间的题是在考察什么玩意儿,,,,,,,
不知道有数重复和不重复会有什么影响啊。
public class Solution {
/*
* @param A: an integer ratated sorted array and duplicates are allowed
* @param target: An integer
* @return: a boolean
*/
public boolean search(int[] A, int target) {
// write your code here
int length = A.length;
if(length <= 0){
return false;
}
if(target < A[0] && target > A[length -1 ]){
return false;
}
int i = 0;
if(target >= A[0]){
while(i < length && A[i] <= target){
if(A[i] == target){
return true;
}
i++;
}
return false;
}
i = length - 1;
if(target <= A[i]){
while(i >= 0 && A[i] >= target){
if(A[i] == target){
return true;
}
i--;
}
}
return false;
}
}