题目:二分查找

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

  1. class Solution {
  2. public:
  3. /**
  4. * @param nums: The integer array.
  5. * @param target: Target number to find.
  6. * @return: The first position of target. Position starts from 0.
  7. */
  8. int binarySearch(vector<int> &array, int target) {
  9.  
  10. int left = 0;
  11. int right = array.size() - 1;
  12.  
  13. while (left + 1 < right) {
  14. int mid = left + (right - left) / 2;
  15. if (array[mid] >= target) {
  16. right = mid;
  17. } else {
  18. left = mid;
  19. }
  20. }
  21.  
  22. if (array[left] == target) {
  23. return left;
  24. }
  25. if (array[right] == target) {
  26. return right;
  27. }
  28.  
  29. return -1;
  30. }
  31. };

  

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

  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题目分析 1

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

  5. jvascript 顺序查找和二分查找法

    第一种:顺序查找法 中心思想:和数组中的值逐个比对! /* * 参数说明: * array:传入数组 * findVal:传入需要查找的数 */ function Orderseach(array,f ...

  6. Java实现的二分查找算法

    二分查找又称折半查找,它是一种效率较高的查找方法. 折半查找的算法思想是将数列按有序化(递增或递减)排列,查找过程中采用跳跃式方式查找,即先以有序数列的中点位置为比较对象,如果要找的元素值小 于该中点 ...

  7. 从一个NOI题目再学习二分查找。

    二分法的基本思路是对一个有序序列(递增递减都可以)查找时,测试一个中间下标处的值,若值比期待值小,则在更大的一侧进行查找(反之亦然),查找时再次二分.这比顺序访问要少很多访问量,效率很高. 设:low ...

  8. java实现二分查找

    /** * 二分查找 * @param a * @param n * @param value * @return * @date 2016-10-8 * @author shaobn */ publ ...

  9. 最新IP地址数据库 二分逼近&二分查找 高效解析800万大数据之区域分布

    最新IP地址数据库  来自 qqzeng.com 利用二分逼近法(bisection method) ,每秒300多万, 比较高效! 原来的顺序查找算法 效率比较低 readonly string i ...

随机推荐

  1. TextView selector 在LinearLayout中获取焦点问题

    通常需要修改textview被选中时文字颜色,总是没效果,有以下几种方式可以实现: <?xml version="1.0" encoding="utf-8" ...

  2. UDP and TCP

    UDP unreliable, just add de-multiplexing and error checking on data than IP. Best effort datagram(数据 ...

  3. 在EF Core里面如何使用以前EntityFramework的DbContext.Database.SqlQuery<SomeModel>自定义查询

    问: With Entity Framework Core removing dbData.Database.SqlQuery<SomeModel> I can't find a solu ...

  4. SQLserver高级编程

    1.数据库设计 数据库设计的重要性: 减少冗余,提高性能.易维护 数据库设计的步骤: 1.收集信息.标识对象.标识属性.标识关系(一对一.一对多.多对一.多对多) E-R图: 属性:定义实体的性质.实 ...

  5. 课时48.表单标签-H5(了解)

    可以自动校验输入的内容是否符合邮箱的格式,我带大家随便看几个就行了,因为大部分浏览器对这几个特性有的支持,有的不支持,所以大家只要了解就好了   

  6. Swift_方法

    Swift_方法 点击查看源码 ///方法 class Methods: NSObject { func test() { // self.testInstanceMethods() //实例方法 s ...

  7. Vue--- 一点车项目 连接数据库 数据使用

    Vue--- 一点车项目  连接数据库 数据使用 后台服务器 返回数据 处理 created 这个钩子在实例被创建之后被调用: async created(){ // 分类 catelist { le ...

  8. 洛谷P4383 [八省联考2018]林克卡特树lct(DP凸优化/wqs二分)

    题目描述 小L 最近沉迷于塞尔达传说:荒野之息(The Legend of Zelda: Breath of The Wild)无法自拔,他尤其喜欢游戏中的迷你挑战. 游戏中有一个叫做“LCT” 的挑 ...

  9. 常用关于时间的一些设置。获取当前时间后30天;判断时间段一年内;Date转String,String转Date

        //获取当前时间后30天(之前也可),天数不限可修改1 var data = new Date(); var date1 = newe Date(date); date2 = date1.se ...

  10. C# 发送Http协议 模拟 Post Get请求

    1.参数 paramsValue的格式 要和 Reques.ContentType一致, 如果 contentype  "application/x-www-form-urlencoded& ...