[LeetCode] questions conclusion_ Binary Search
Binary Search
T(n) = T(n/2) + O(1) => T(n) = O(lg n)
proof:

如果能用iterable , 就用while loop, 可以防止用recursion的时候stack overflow( process in Linux is 8Mb), stack room is static for each process. (堆空间, heap room 是内存, 是动态的。)层数越多,存储更多中间/临时变量,最终超过系统配的stack空间,就会出现stack overflow。
建议:
- 如果问题不复杂,能用iterable就用iterable
- 如果问题比较复杂,还是用recursive吧
1) ask 是否有duplicates?
1.1 如果没有duplicates
标准的binary search:
a) l + 1 < r 相当于如果l, r 相邻就跳出,这样能够避免死循环. 但是while loop之后需要加一个判断, 也就是d选项
b) l + (r - l)//2 # 不用(l+r)//2 , 因为l + r 有可能overflow,强行装*,当l + r 之和超过int的最大值,2的32次方减1,会溢出。
c) A[mid] ==, <, > # 为了bug free, 建议举个小栗子, 然后画图去判断.
d) A[l], A[r] ? target
Code
def BinarySearch(self, nums, target): # nums is sorted
if not nums or target < nums[0] or target > nums[-1]: return -1
l, r = 0, len(nums) -1 # note here is len(nums) -1
while l + 1 < r:
mid = l + (r - l)//2
if nums[mid] > target:
r = mid
elif nums[mid] < target:
l = mid
else:
return mid
if nums[l] == target:
return l
if nums[r] == target:
return r
return -1
1.2 如果有duplicates
a) ask 是first index, last index or dont matter?
如果是first index:
if A[mid] == target: r = mid
最后判断 A[l] 和A[r] 的时候先判断 A[l]
如果是last index:
if A[mid] == target: l = mid
最后判断 A[l] 和A[r] 的时候先判断 A[r]
Questions:
[LeetCode] 704. Binary Search_Easy tag: Binary Search
[LeetCode] 374. Guess Number Higher or Lower_Easy tag: Binary Search
[LeetCode] 34. Find First and Last Position of Element in Sorted Array == [LintCode] 61. Search for a Range_Easy tag: Binary Search first index and last index == target in duplicates.
[LeetCode] 35. Search Insert Position_Easy tag: Binary Search first index >= target, without duplicates
[LeetCode] 278. First Bad Version_Easy tag: Binary Search first index, without duplicates
[LeetCode] 162. Find Peak Element(852. Peak Index in a Mountain Array)_Medium tag: Binary Search
[LeetCode] 74. Search a 2D Matrix_Medium tag: Binary Search
[LeetCode] 240. Search a 2D Matrix II_Medium tag: Binary Search
[LeetCode] 69. Sqrt(x)_Easy tag: Binary Search
[LeetCode] 153. Find Minimum in Rotated Sorted Array_Medium tag: Binary Search first index <= nums[-1]
[LeetCode] 154. Find Minimum in Rotated Sorted Array II_Hard tag: not real binary search anymore
[LeetCode] 33. Search in Rotated Sorted Array_Medium tag: Binary Search
[LeetCode] 81. Search in Rotated Sorted Array II_Medium tag: not real binary search anymore
[LeetCode] 702. Search in a Sorted Array of Unknown Size_Medium tag: Binary Search
[LeetCode] 4. Median of Two Sorted Arrays_Hard tag: Array, Binary Search
[LeetCode] questions conclusion_ Binary Search的更多相关文章
- [LeetCode] 95. Unique Binary Search Trees II 唯一二叉搜索树 II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- [LeetCode] 96. Unique Binary Search Trees 唯一二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- [LeetCode] 272. Closest Binary Search Tree Value II 最近的二叉搜索树的值 II
Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...
- [leetcode]95. Unique Binary Search Trees II给定节点形成不同BST的集合
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...
- [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆
Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...
- Java for LeetCode 095 Unique Binary Search Trees II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- [LeetCode] 98. Validate Binary Search Tree_Medium
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- [LeetCode] 270. Closest Binary Search Tree Value 最近的二叉搜索树的值
Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...
- [LeetCode] Trim a Binary Search Tree 修剪一棵二叉搜索树
Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that a ...
随机推荐
- js dom 观察者属性 MutationObserver
MDN上说的很清楚 MutationObserver给开发者们提供了一种能在某个范围内的DOM树发生变化时作出适当反应的能力.该API设计用来替换掉在DOM3事件规范中引入的Mutation事件 co ...
- 拓展 NLog 优雅的输送日志到 Logstash
在上上篇博客通过对aspnetcore启动前配置做了一些更改,以及对nlog进行了自定义字段,可以把请求记录输送到mysql,正式情况可能不会这么部署.因为近期也在学习elk,所以就打算做一个实例,结 ...
- 关于OpenJDK和Orcale JDK区别
一.环境Centos 今天搞tomcat发现了一个问题,众所周知,tomcat需要java环境支持,然后我今天就想着尝试yum安装java,命令 yum install -y java* 确实可以安装 ...
- 笔记本串口连接IBM 小机
首先要有一根两头母的九针串口线&USB转串口线其次,配置波特率19200.数据位8.停止位1.无校验位.流控:Xon/Xoff P4是9600,P5 P6都是19200了( 默认是19200波 ...
- db2 reorg到底需要多少表空间(转)
脱机reorg需要一定的空间,这个空间与目标所在的数据表空间.索引表空间.以及临时表空间均有关,各空间需求的大小与表和索引所占用的数据页和索引页相关. (1)对表执行reorg操作如:db2 reor ...
- 在CentOS中安装arial字体
验证码不能正常显示是因为 linux 没有字体 1. widonws下载字体文件到Linux windows的字体比较多,其字体文件位于 C:\WINDOWS\Fonts . 从其中copy相应的字体 ...
- hbase与hive集成:hive读取hbase中数据
1.创建hbase jar包到hive lib目录软连接 hive需要jar包: hive-hbase-handler-0.13.1-cdh5.3.6.jar zookeeper-3.4.5-cdh5 ...
- Cookie映射
Cookie映射 第 5 章 Cookie映射 http://amp.ad.sina.com.cn/sax/doc/zh-CN/xhtml/bk01pt02ch05.xhtml 第 5 章 Cooki ...
- MySQL之更新型触发器
DELIMITER || CREATE TRIGGER tri_video_class AFTER UPDATE ON 数据库名.表名称 FOR EACH ROW begin IF((old.stat ...
- Verilog HDL语言实现的单周期CPU设计(全部代码及其注释)
写在前面:本博客为本人原创,严禁任何形式的转载!本博客只允许放在博客园(.cnblogs.com),如果您在其他网站看到这篇博文,请通过下面这个唯一的合法链接转到原文! 本博客全网唯一合法URL:ht ...