Topic describes

Their thinking

If the value is larger than the left element, the value is larger than the right element. If the value is larger than the left element, the value is larger than the right element. If the value is larger than the left element, the value is larger than the right element.

The problem solving code

var search = function (nums, target) {
    let left = 0;
    let right = nums.length - 1;
    let mid = left + right >>> 1;
    // Start looking for the median
    if (nums.length > 2) {
        for (let i = 1; i < nums.length; i++) {
            if (nums[i] > nums[i-1] && nums[i] > nums[i+1]) {
                mid = i;
                break;
            }
            if (nums[i] < nums[i-1] && nums[i] < nums[i+1]) {
                mid = i-1;
                break;
            }
        }
    }
    mid
    if (target < nums[mid]) {
        // nums.slice(mid)
        if (target < nums[0] && target <= nums[nums.length - 1]) {
            if(nums.slice(mid).indexOf(target) ! = = -1) {
                const temp = nums.slice(mid);
                return temp.indexOf(target) + mid
            } else {
                return -1; }}else {
            const temp = nums.slice(0, mid);
            return temp.indexOf(target)
        }
    } else if (target === nums[mid]) {
        return mid;
    } else {
        if (target > nums[0] && target <= nums[nums.length - 1]) {
            if(nums.slice(mid).indexOf(target) ! = = -1) {
                const temp = nums.slice(mid);
                return temp.indexOf(target) + mid
            } else {
                return -1; }}else {
            return -1}}};Copy the code

revelation

  • Learn how to find the median by using bitwise operations.
  • Learn to peak.

Through the screenshots