LeetCode - Binary Search 总结

二分法简单但是边界值十分容易出错, 时间复杂度为O(log n)
mid = right + (left - right) / 2 防止left right 都大时候溢出
二分法剖析:Binary Search 二分搜索法 C++

bold - high frequency top 5

 

第一类 基本用法

Search Insert Position [easy, 2]

Find First and Last Position of Element in Sorted Array [medium, 2]

First Bad Version [easy, 2]

Find Peak Element [medium, 2]

Heaters [easy, 2] (解法二,第一个不小于)

Closest Binary Search Tree Value

Median of Two Sorted Arrays [hard, 1]

 

 

第二类 数学相关

Sqrt(X) [easy, 2]

Valid Perfect Square [easy, 3] (解法二,==)

Pow(x, n) [median, 1]

Arranging Coins [easy, 2] (数列,小心overflow)

 

 

第三类 2D binary search

Search a 2d Matrix [median, 2], II[median, 2] (not BS)

 

第四类 sorted or rotated array

Search in Rotated Sorted Array [median, 1], II [median, 2] (duplicated allowed)

Find Minimum in Rotated Sorted Array, II (duplicated allowed)

 

利用Binary Search维护Longest Increasing Substring

维护一个vector,每一个元素代表 increasing substring position i 上最小的数
update: 小于首元素代替,大于末元素加在末尾,大于首元素小于末元素代替第一个比它大的元素
注:这个维护的vector并非最后的increasing substring,只是元素个数相同

Longest Increasing Subsequence [medium, 2]

Russian Doll Envelopes [hard, 2] (上面的升级版)

还想练手:

Increasing Triplet Subsequence [medium, 3]

 

 

未归类

Find Smallest Letter Greater Than Target

Find Kth Smallest Pair Distance

Find K Closest Elements

Find the Duplicate Number

Kth Smallest Number in Multiplication Table

Kth Smallest Element in a BST

Kth Smallest Element in a Sorted Matrix

Count Complete Tree Nodes

Divide Two Integers

Find Right Interval

4Sum II

Maximum Length of Repeated Subarray

Minimum Size Subarray Sum

Max Sum of Rectangle No Larger Than K [hard, 3]

Split Array Largest Sum 
Kth Smallest Element in a Sorted Matrix
Count of Smaller Numbers After Self

 

 

Reference:

https://lefttree.gitbooks.io/leetcode-categories/BinarySearch/binarySearch.html

https://blog.csdn.net/linhuanmars/article/details/31354941

https://zxth93.github.io/2017/11/20/LeetCode-binary-search类总结/index.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值