图片说明

/**
  *
  * @param A int整型一维数组
  * @param target int整型
  * @return int整型
  */
function search( A ,  target ) {
    // write code here
    let index = -1;
    let left  = 0;
    let right = A.length - 1;

    // 没有旋转
    if (A[left] <= A[right]) {
        return binarySearch(A, left , right, target);
    } else {
        // 有旋转,找到旋转点,确定两个有序子数组
        while (A[left] > A[right]) {
            let mid = (left + right) >> 1;
            if (A[mid] > A[left]) {
                left = mid;
            } else {
                right = mid;
            }
        }
        if (target >= A[0]) {
            return binarySearch(A,0,left,target);
        } else {
            return binarySearch(A,left + 1,A.length - 1,target);
        }
    }





}
function binarySearch(A, left, right, target) {
    if (A == null || A.length == 0) {
        return -1;
    }
    while (left <= right) {
        let mid = (left + right) >> 1;
        if (target < A[mid]) {
            right  = mid -1;
        } else if (target > A[mid]) {
            left = mid + 1;
        } else {
            return mid;
        }
    }
    return -1;
}
module.exports = {
    search : search
};