📝【LeetCode】一个模板通杀所有「二分查找」问题
Metadata
- Author: Images。
- Full Title: 📝【LeetCode】一个模板通杀所有「二分查找」问题
- Category: articles
- URL: https://imageslr.com/2020/03/15/binary-search.html#%E9%97%AE%E9%A2%98%E5%AE%9A%E4%B9%89
Highlights
- left, right := 0, len(nums)-1 for left ⇐ right (View Highlight)
- if nums[mid] >= target { // 这里的比较运算符与题目要求一致 right = mid - 1 } else { left = mid + 1 } (View Highlight)
- 然后将下界的下标减一,就是我们要找的上界: // 查找满足 x ≤ target 的上界的下标 func UpperBound(nums []int, target int) int { return LowerBound(nums, target)-1 } (View Highlight)
- 查找指定值第一次出现的位置 (View Highlight)