二分查找

给定一个排序的整数数组(升序)和一个要查找的整数target,用O(logn)的时间查找到target第一次出现的下标(从0开始),如果target不存在于数组中,返回-1。

样例

在数组 [1, 2, 3, 3, 4, 5, 10] 中二分查找3,返回2。

挑战

如果数组中的整数个数超过了2^32,你的算法是否会出错?

标签

二分法 数组

说明

普通的二分查找,数组中整数超过2^32会导致算法出错。

code

class Solution {
public:
/**
* @param nums: The integer array.
* @param target: Target number to find.
* @return: The first position of target. Position starts from 0.
*/
int binarySearch(vector<int> &array, int target) {
// write your code here
int size = array.size();
int low = 0, high = size-1, mid = (high + low) / 2;
int find = -1; if(array[low]>target || array[high]<target) {
return -1;
} while(low <= high) {
if(array[mid] == target) {
find = mid;
break;
}
else if(array[mid] < target)
low = mid + 1;
else
high = mid - 1; mid = (high + low) / 2;
} while(find > 0) {
if(array[find-1] == target)
find--;
else
break;
} return find;
}
};

改进思路

若数组长度大于2^32,则low, mid, high三个数组下标均有可能超出int的表示范围,可以采取的改进思路是:

  1. 拆分array数组,使每个子数组的长度均小于2^32,每个子数组长度为size
  2. 将target与每个子数组的头尾比较,若存在target大于此数组头部且小于此数组尾部,则对此数组进行二分查找

lintcode-14-二分查找的更多相关文章

  1. (二分查找 拓展) leetcode 162. Find Peak Element && lintcode 75. Find Peak Element

    A peak element is an element that is greater than its neighbors. Given an input array nums, where nu ...

  2. (二分查找 拓展) leetcode 34. Find First and Last Position of Element in Sorted Array && lintcode 61. Search for a Range

    Given an array of integers nums sorted in ascending order, find the starting and ending position of ...

  3. lintcode:Binary Search 二分查找

    题目: 二分查找 给定一个排序的整数数组(升序)和一个要查找的整数target,用O(logn)的时间查找到target第一次出现的下标(从0开始),如果target不存在于数组中,返回-1. 样例 ...

  4. lintcode 二分查找

    题目:二分查找 描述:给定一个排序的整数数组(升序)和一个要查找的整数target,用O(logn)的时间查找到target第一次出现的下标(从0开始),如果target不存在于数组中,返回-1. c ...

  5. 巨蟒python全栈开发-第14天 内置函数2 递归 二分查找

    一.今日内容总览 1.内置函数补充 repr() 显示出字符串的官方表示形式 chr() arscii码中的字,转换成位置 ord() arscii码中的位置,转换成字2.递归 自己调用自己 两个口: ...

  6. 二分查找总结及部分Lintcode题目分析 1

    进行二分查找课程回顾与总结,包括以下几个方面,二分法的模板总结和解题思路.应用. 二分法模板总结classical binary search: 1. 必须要做的排除极端情况,也就是数组(用A表示)不 ...

  7. PHP 二分查找(详细)

    <?php //        PHP 二分查找 function search($arr, $sea){ $low = 0;                // 确定数组的开始的下标 $len ...

  8. java for循环和数组--冒泡排序、二分查找法

    //100以内与7相关的数   for(int a=1;a<=100;a++){    if(a%7==0||a%10==7||a/10==7){     System.out.print(a+ ...

  9. 分蛋糕(C - 二分查找)

    分蛋糕 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85904#problem/C Description My birthd ...

  10. 基于visual Studio2013解决面试题之1307二分查找

     题目

随机推荐

  1. PHP中使用foreach时加&符号的用法

    foreach时加&符号:遍历的同时改变原数组即修改数据或者增加数据. $arr = ['a', 'b', 'c']; foreach ($arr as $key => &$va ...

  2. python3.X 安装web.py 失败的解决方法

    python2.x 安装python是非常顺利的 但是 在进行 pip3 install web.py 时提示很多错误 例如缺少模块 语法错误...... 最后试了一下web.py 的dev版本 pi ...

  3. Windows 聚焦(锁屏背景)不更新的解决方法

    在 Windows Store 搜索 Dynamic theme 安装后可对桌面背景.锁屏界面等进行设置,非常好用!

  4. Linux字符设备学习,总结

    注册字符驱动的一种老方法: 注册一个字符设备的经典方法是使用:int register_chrdev(unsigned int major, const char *name, structfile_ ...

  5. Zabbix 3.4.11版本 自定义监控项

    一.实验思路过程 创建项目.触发器.图形,验证监控效果: Template OS Linux 模板基本涵盖了所有系统层面的监控,包括了我们最关注的 几项:ping.load.cpu 使用率.memor ...

  6. Java基础之static关键字的用法

    Java中的static关键字主要用于内存管理.我们可以应用static关键字在变量.方法.块和嵌套类中. static关键字属于类,而不是类的实例.        静态(static)可以是: 变量 ...

  7. 北京Uber优步司机奖励政策(3月16日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  8. [python]安装wxpython的时候遇到问题记录

    一.安装wxpython的时候报错 “no installation of python 2.7 found in registy” 解决方案: win7上,已经安装python27,但是在安装wxp ...

  9. WPF Style Setter use a TemplateBinding?

    <Style TargetType="{x:Type local:ImageButton}"> <Setter Property="Horizontal ...

  10. mysql 开启远程连接

    如图,修改mysql数据库中user表中的User字段为root的host为%,然后重新启动mysql服务即可让远程桌面连接本地.