二分查找算法是最常用的一种高效算法,所以本文将常见的情形做一个总结,得到一个二分查找的模板,方便应对各种二分查找中的问题。

当前有一个有序的数列:

1, 5, 9   【每个数字都是唯一的】
1, 2, 2, 9 【存在重复的数字】

模板

该模板可以在数列中查找一个数target,如果target在数列中存在,输出target第一次出现位置下标,如果不存在,则输出插入到数列中之后的下标。

int binarySearch(vector<int>& numbers, int target) {
int len = numbers.size();
int l = 0, r = len, mid = l+(r-l)/2;
while (l < r) {
if (numbers[mid] >= target) {
r = mid;
} else {
l = mid+1;
}
mid = l+(r-l)/2;
}
return r;
} // 样例 数列: 1 5 9
target : 1 output : 0
target : 5 output : 1
target : 9 output : 2
target : 0 output : 0
target : 10 output : 3
target : 4 output : 1 数列: 1 2 2 9
target : 1 output : 0
target : 2 output : 1
target : 9 output : 3
target : 0 output : 0
target : 10 output : 4
target : 4 output : 3

binary search模板总结的更多相关文章

  1. LintCode Search For a Range (Binary Search)

    Binary Search模板: mid 和 target 指针比较,left/ right 和 target 比较. 循环终止条件: 最后剩两数比较(while(left + 1 < righ ...

  2. 【Recover Binary Search Tree】cpp

    题目: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without chan ...

  3. Binary Search 二分法方法总结

    Binary Search 二分法方法总结 code教你做人:二分法核心思想是把一个大的问题拆成若干个小问题,最重要的是去掉一半或者选择一半. 二分法模板: public int BinarySear ...

  4. [LeetCode] 35. Search Insert Position_Easy tag: Binary Search

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  5. 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 clo ...

  6. 669. Trim a Binary Search Tree修剪二叉搜索树

    [抄题]: Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so ...

  7. 501. Find Mode in Binary Search Tree查找BST中的众数

    [抄题]: Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently oc ...

  8. 153. Find Minimum in Rotated Sorted Array(leetcode, binary search)

    https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/ leetcode 的题目,binary ...

  9. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

随机推荐

  1. Luogu P1265 公路修建

    一眼看去,就是一道MST的模板题. 然后果断准备跑Kruskal,然后5个TLE. Kruskal复杂度对于这个完全图要O(n^2*logn^2),快排就会导致超时. 然后打了刚学的Prim.朴素O( ...

  2. Luogu P1341 无序字母对

    突然发现我现在很喜欢打图论题. 然而都是很easy的. 这道题很坑,用C++打了一遍莫名Too many or too few lines. 然后我打出了我的独门绝技Pascal.这可能是我最后一次用 ...

  3. Quartz_配置

    quartz_jobs.xml job 任务 其实就是1.x版本中的<job-detail>,这个节点是用来定义每个具体的任务的,多个任务请创建多个job节点即可 name(必填) 任务名 ...

  4. Redis常用操作-------Set(集合)

    1.SADD key member [member ...] 将一个或多个 member 元素加入到集合 key 当中,已经存在于集合的 member 元素将被忽略. 假如 key 不存在,则创建一个 ...

  5. 解决AJAX session跨域失效

    1.想实现的功能是登录时有个验证码,这个验证码后台提供,然后放在session中,前台把用户输入的验证码通过AJAX发给后台,后台把session中的验证码取出来然后比较不同,一样则通过. 问题出现在 ...

  6. github作业

    链接:   https://github.com/liuyu13/liuyu13-1 总结:git可以学习的东西还有很多.git协议,分布式协作,git项目管理,git技巧,github的使用和实践, ...

  7. MCMC等采样算法

    一.直接采样 直接采样的思想是,通过对均匀分布采样,实现对任意分布的采样.因为均匀分布采样好猜,我们想要的分布采样不好采,那就采取一定的策略通过简单采取求复杂采样. 假设y服从某项分布p(y),其累积 ...

  8. Install Kernel 3.10 on CentOS 6.5

    http://bicofino.io/2014/10/25/install-kernel-3-dot-10-on-centos-6-dot-5/ https://gree2.github.io/lin ...

  9. 同步或者重构Activiti Identify用户数据的多种方案比较

    http://www.kafeitu.me/activiti/2012/04/23/synchronize-or-redesign-user-and-role-for-activiti.html 如何 ...

  10. php常用扩展安装

    ####memcache wget http://pecl.php.net/get/memcache-2.2.7.tgztar xf memcache-2.2.7.tgz cd memcache-2. ...