1、Java AC 1
public class Solution { public int search(int[] A, int target) { if(A == null || A.length == 0){ return 0; } int len = A.length; if(target > A[0]){ int i = 1; while(i < len && A[i-1] < A[i] && target > A[i]){ i++; } if (i >= len) { return -1; } if(target == A[i]){ return i; }else{ return -1; } }else if(target < A[0]){ int i = len - 1; while(i > 0 && A[i] > A[i-1] && target < A[i]){ i--; } if (i <= 0) { return -1; } if(target == A[i]){ return i; }else{ return -1; } } return 0; } }1、Java AC 2
public class Solution { public int search(int[] A, int target) { if(A == null || A.length == 0){ return 0; } int len = A.length; int low = 0; int high = len-1; int mid = -1; while(low <= high){ mid = (low + high) >> 1; if(A[mid] == target){ return mid; } if(A[mid] > A[low]){ if(A[mid] > target && A[low] <= target){ high = mid - 1; }else{ low = mid + 1; } }else if(A[mid] < A[low]){ if(target > A[mid] && target <= A[high] ){ low = mid + 1; }else{ high = mid - 1; } }else{ low ++; } } return -1; } }2、Java AC 1
public class Solution { public boolean search(int[] A, int target) { if(A == null || A.length == 0){ return false; } int len = A.length; if(target > A[0]){ int i = 1; while(i < len && A[i-1] <= A[i] && target > A[i]){ i++; } if (i >= len) { return false; } if(target == A[i]){ return true; }else{ return false; } }else if(target < A[0]){ int i = len - 1; while(i > 0 && A[i] >= A[i-1] && target < A[i]){ i--; } if (i <= 0) { return false; } if(target == A[i]){ return true; }else{ return false; } } return true; } }2、Java AC 2
public class Solution { public boolean search(int[] A, int target) { if(A == null || A.length == 0){ return false; } int len = A.length; int low = 0; int high = len-1; int mid = -1; while(low <= high){ mid = (low + high) >> 1; if(A[mid] == target){ return true; } if(A[mid] > A[low]){ if(A[mid] > target && A[low] <= target){ high = mid - 1; }else{ low = mid + 1; } }else if(A[mid] < A[low]){ if(target > A[mid] && target <= A[high] ){ low = mid + 1; }else{ high = mid - 1; } }else{ low ++; } } return false; } }