lintcode 二分查找
题目:二分查找
描述:给定一个排序的整数数组(升序)和一个要查找的整数target
,用O(logn)
的时间查找到target第一次出现的下标(从0开始),如果target不存在于数组中,返回-1
。
- 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) {
- int left = 0;
- int right = array.size() - 1;
- while (left + 1 < right) {
- int mid = left + (right - left) / 2;
- if (array[mid] >= target) {
- right = mid;
- } else {
- left = mid;
- }
- }
- if (array[left] == target) {
- return left;
- }
- if (array[right] == target) {
- return right;
- }
- return -1;
- }
- };
lintcode 二分查找的更多相关文章
- (二分查找 拓展) 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 ...
- (二分查找 拓展) 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 ...
- lintcode:Binary Search 二分查找
题目: 二分查找 给定一个排序的整数数组(升序)和一个要查找的整数target,用O(logn)的时间查找到target第一次出现的下标(从0开始),如果target不存在于数组中,返回-1. 样例 ...
- 二分查找总结及部分Lintcode题目分析 1
进行二分查找课程回顾与总结,包括以下几个方面,二分法的模板总结和解题思路.应用. 二分法模板总结classical binary search: 1. 必须要做的排除极端情况,也就是数组(用A表示)不 ...
- jvascript 顺序查找和二分查找法
第一种:顺序查找法 中心思想:和数组中的值逐个比对! /* * 参数说明: * array:传入数组 * findVal:传入需要查找的数 */ function Orderseach(array,f ...
- Java实现的二分查找算法
二分查找又称折半查找,它是一种效率较高的查找方法. 折半查找的算法思想是将数列按有序化(递增或递减)排列,查找过程中采用跳跃式方式查找,即先以有序数列的中点位置为比较对象,如果要找的元素值小 于该中点 ...
- 从一个NOI题目再学习二分查找。
二分法的基本思路是对一个有序序列(递增递减都可以)查找时,测试一个中间下标处的值,若值比期待值小,则在更大的一侧进行查找(反之亦然),查找时再次二分.这比顺序访问要少很多访问量,效率很高. 设:low ...
- java实现二分查找
/** * 二分查找 * @param a * @param n * @param value * @return * @date 2016-10-8 * @author shaobn */ publ ...
- 最新IP地址数据库 二分逼近&二分查找 高效解析800万大数据之区域分布
最新IP地址数据库 来自 qqzeng.com 利用二分逼近法(bisection method) ,每秒300多万, 比较高效! 原来的顺序查找算法 效率比较低 readonly string i ...
随机推荐
- TextView selector 在LinearLayout中获取焦点问题
通常需要修改textview被选中时文字颜色,总是没效果,有以下几种方式可以实现: <?xml version="1.0" encoding="utf-8" ...
- UDP and TCP
UDP unreliable, just add de-multiplexing and error checking on data than IP. Best effort datagram(数据 ...
- 在EF Core里面如何使用以前EntityFramework的DbContext.Database.SqlQuery<SomeModel>自定义查询
问: With Entity Framework Core removing dbData.Database.SqlQuery<SomeModel> I can't find a solu ...
- SQLserver高级编程
1.数据库设计 数据库设计的重要性: 减少冗余,提高性能.易维护 数据库设计的步骤: 1.收集信息.标识对象.标识属性.标识关系(一对一.一对多.多对一.多对多) E-R图: 属性:定义实体的性质.实 ...
- 课时48.表单标签-H5(了解)
可以自动校验输入的内容是否符合邮箱的格式,我带大家随便看几个就行了,因为大部分浏览器对这几个特性有的支持,有的不支持,所以大家只要了解就好了
- Swift_方法
Swift_方法 点击查看源码 ///方法 class Methods: NSObject { func test() { // self.testInstanceMethods() //实例方法 s ...
- Vue--- 一点车项目 连接数据库 数据使用
Vue--- 一点车项目 连接数据库 数据使用 后台服务器 返回数据 处理 created 这个钩子在实例被创建之后被调用: async created(){ // 分类 catelist { le ...
- 洛谷P4383 [八省联考2018]林克卡特树lct(DP凸优化/wqs二分)
题目描述 小L 最近沉迷于塞尔达传说:荒野之息(The Legend of Zelda: Breath of The Wild)无法自拔,他尤其喜欢游戏中的迷你挑战. 游戏中有一个叫做“LCT” 的挑 ...
- 常用关于时间的一些设置。获取当前时间后30天;判断时间段一年内;Date转String,String转Date
//获取当前时间后30天(之前也可),天数不限可修改1 var data = new Date(); var date1 = newe Date(date); date2 = date1.se ...
- C# 发送Http协议 模拟 Post Get请求
1.参数 paramsValue的格式 要和 Reques.ContentType一致, 如果 contentype "application/x-www-form-urlencoded& ...