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的更多相关文章

  1. [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 ...

  2. [LeetCode] 96. Unique Binary Search Trees 唯一二叉搜索树

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  3. [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 ...

  4. [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 ...

  5. [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆

    Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...

  6. 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 ...

  7. [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 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. day_5.18_py总结

  2. STL之map学习实例

    ``` #include<iostream> #include<algorithm> #include<vector> #include<map> #i ...

  3. css学习_css书写规范

    css书写规 1.空格规范: div { color: red; } 2.选择器规范 3.属性 属性定义要另起一行 属性定义后必须以分号结尾 div { color: red; font-size: ...

  4. {MySQL存储引擎介绍}一 存储引擎解释 二 MySQL存储引擎分类 三 不同存储引擎的使用

    MySQL存储引擎介绍 MySQL之存储引擎 本节目录 一 存储引擎解释 二 MySQL存储引擎分类 三 不同存储引擎的使用 一 存储引擎解释 首先确定一点,存储引擎的概念是MySQL里面才有的,不是 ...

  5. 命令配置linux分辨率

    1. xrandr 使用该命令列举系统支持的分辨率 2. xrandr -s 回复原来的分辨率 3. xrandr -s 1360x768 设置分辨率   如果分辨率没能锁定,请在根目录使用gedit ...

  6. UILabel中NSAttributedString和其LinebarkModel等属性之间的关系

    如果设置了一个富文本给一个UILabel,那么后续改变这个UILabel的属性时将会同时改变UILabel.attributedText的属性描述,此时若改变了其它的大小.换行模式(如果在显示时我们可 ...

  7. 关于nginx重新编译

    nginx安装成功后,发现有一些其他模块没有编译进去,或者想额外添加一些模块,这时候就要重新编译nginx. 首先,查看之前编译的一些参数,比如: 1 2 3 4 5 [root@lmode ngin ...

  8. [https][ssl] keyless SSL

    HTTP Server 集群前的负载设备,或内容审计设备等,在处理https的时候,需要用户配置提供证书. 但是考虑到安全问题,HTTP Server并不愿意把证书配置到其他设备上. 这个时候,就有个 ...

  9. [development] __attribute__((weak))是干嘛的

    简单的说,就是当发生 “重复定义的时候”.被声明者会被冲突者覆盖掉. 这里还涉及了weak与alias连用的情况. 参见,里边有两个例子,很浅显易懂. https://my.oschina.net/s ...

  10. day4_高效处理文件

    read()将文件内容从磁盘中全部读出,放到内存,再给cpu处理,性能低,如果文件量大,很容易内存溢出或卡死. 高效方式: 方式一:一般不用的,代码行多 f = open('users.txt','r ...