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

你可能感兴趣的:(LeetCode总结)