[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 ...
随机推荐
- 6.29一个_rcv 面试题
#coding:utf-8 #2018-6-29 16:30:34 #类调用属性,属性没有,用__getatrr__魔法方法! #目的打印出 think different itcast class ...
- linux 下实用软件工具推荐
截图:Deepin-Screenshot 音乐:deepin-music netease-music 绘图工具:Draw.io Desktop (chrome extension) / www.pro ...
- PHP基础语法 【】【】 PHP函数
<?php //注释语法 /* 多行注释 */ //输出语法 echo "hello"; echo "world","dhakj"; ...
- Linux 创建文件系统及挂载文件系统流程详解(转)
作者:北南南北 来自: LinuxSir.Org 摘要:本文对新增硬盘,切割硬盘,创建硬盘分区,为硬盘分区创建文件系统,以及加载文件系统的流程做总结性论述:主要是为初学者弄清楚这一操作过程:本文涉及f ...
- js常用校验
//验证金钱数字obj.regexMoney = function (money) { var reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)| ...
- 使用Entity Framework Core需要注意的一个全表查询问题
.NET Core 迁移工作如火如荼,今天在使用 Entity Frameowork Core(又名EF Core)时写了下面这样的 LINQ 查询表达式: .Where(u => u.Id = ...
- Imu_tk算法流程及数据采集要求和标定程序参数设置
Imu_tk算法流程 由于VIO中,普遍使用的是精度较低的imu,所以其需要一个较为准确的内参数和noise的估计.Noise大家通常使用Allan方差进行估计可以得到较为可信的结果,这里不赘述了.内 ...
- POJ 3974 - Palindrome - [字符串hash+二分]
题目链接:http://poj.org/problem?id=3974 Time Limit: 15000MS Memory Limit: 65536K Description Andy the sm ...
- [No0000D6]端口-进程查询.bat
@echo off color a Title XP端口-进程查询 setlocal enabledelayedexpansion echo ╔- -╗ echo 本机开放的端口及使用该端口的进程 e ...
- MyCAT 在 Cobar 的基础上,完成了彻底的 NIO 通讯,并且合并了两个线程池
研读: 1.http://www.mycat.io <Mycat权威指南> 第 2 章 Mycat 前世今生: 浏览: 深度认识 Sharding-JDBC:做最轻量级的数据库中间层 - ...