Leetcode刷题——二分查找(4/16)

本文介绍了几种使用二分查找算法解决实际问题的实例,包括求平方根、寻找比目标字母大的最小字母、找出有序数组中的单一元素、定位第一个错误的版本、寻找旋转排序数组中的最小值以及在排序数组中查找元素范围。每个问题的解决方案都给出了详细的代码实现,展示了二分查找在处理数组和字符串时的高效性和灵活性。
摘要由CSDN通过智能技术生成

开始刷题

  • 69. Sqrt(x) (Easy)
  • 744. Find Smallest Letter Greater Than Target (Easy)
  • 540. Single Element in a Sorted Array (Medium)
  • 278. First Bad Version (Easy)
  • 153. Find Minimum in Rotated Sorted Array (Medium)
  • 34. Find First and Last Position of Element in Sorted Array

69. x 的平方根

var mySqrt = function (x) {
    let left = 0;
    let right = x;
    while (left <= right) {
        let mid = Math.floor(left + (right - left) / 2);
        let sqrt = Math.floor(x / mid);
        if (mid === sqrt) {
            return mid;
        } else if (mid > sqrt) {
            right = mid - 1;
        } else {
            left = mid + 1;
        }
    }
    return right;
};

744. 寻找比目标字母大的最小字母

var nextGreatestLetter = function (letters, target) {
    let left = 0;
    let right = letters.length - 1;
    while (left <= right) {
        const mid = Math.floor(left + (right - left) / 2);
        if (letters[mid] <= target) {
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }
    return left >= letters.length ? letters[0] : letters[left];
};

540. 有序数组中的单一元素

tips:通过判断 mid 是与左边相等还是和右边相等,然后根据 mid 的奇偶情况分类。

var singleNonDuplicate = function (nums) {
    let left = 0, right = nums.length - 1;
    while (left < right) {
        const mid = Math.floor(left + (right - left) / 2);
        const isEven = mid % 2 === 1;
        if (nums[mid] === nums[mid - 1]) {
            if(isEven)left = mid + 1;
            else right = mid - 2;
        } else if (nums[mid] === nums[mid + 1]) {
            if(isEven) right = mid - 1;
            else left = mid + 2;
        } else {
            return nums[mid];
        }
    }
    return nums[left];
};

278. 第一个错误的版本

var solution = function (isBadVersion) {
    /**
     * @param {integer} n Total versions
     * @return {integer} The first bad version
     */
    return function (n) {
        let left = 1;
        let right = n;
        while (left < right) {
            const mid = Math.floor(left + (right - left) / 2);
            if (isBadVersion(mid)) {
                right = mid;
            } else {
                left = mid + 1;
            }
        };
        return left;
    };
}

153. 寻找旋转排序数组中的最小值

tips:和最后一个值比

var findMin = function (nums) {
    let left = 0;
    let right = nums.length - 1;
    while (left < right) {
        const mid = Math.floor(left + (right - left) / 2);
        if (nums[mid] > nums[nums.length - 1]) {
            left = mid + 1;
        } else {
            right = mid;
        }
    }
    return nums[left]
};

34. 在排序数组中查找元素的第一个和最后一个位置

tips:两次二分查找,第一次找元素的第一个,第二次找最后一个(也就是下一个元素的第一个的索引减一)

var searchRange = function (nums, target) {
    let ans = [-1, -1];
    let leftIndex = binarySearch(nums, target);
    let rightIndex = binarySearch(nums, target + 1) - 1;
    if (leftIndex < nums.length && nums[leftIndex] == target) {
        return ans = [leftIndex, Math.max(leftIndex,rightIndex)];
    }
    return ans;
};

function binarySearch(nums, target) {
    let left = 0, right = nums.length;
    while (left < right) {
        const mid = left + Math.floor((right - left) / 2);
        if (nums[mid] < target) {
            left = mid + 1;
        } else {
            right = mid;
        }
    }
    return left;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值