Search in Rotated Sorted Array

public class Solution {
    public int search(int[] A, int target) {
		// Start typing your Java solution below
		// DO NOT write main() function
		if(A.length == 0)
			return -1;
		int begin = 0;
		int end = A.length - 1;
		while(begin <= end){
			int mid = begin + (end - begin) / 2;
			if(A[mid] == target)
				return mid;
			else if(A[begin] <= A[mid]){
				if(target >= A[begin] && target <= A[mid])
					end = mid - 1;
				else
					begin = mid + 1;
			}
			else{
				if(target >= A[mid] && target <= A[end])
					begin = mid + 1;
				else
					end = mid - 1;
			}
		}
		return -1;
	}
}

你可能感兴趣的:(Search in Rotated Sorted Array)