基础题之一,是混迹于各种难题的基础,有时会在小公司的大题见到,但更多的是见于选择题。。。

题意:在一个有序数列中,要插入数target,找出插入的位置。

楼主在这里更新了《二分查找综述》第一题的解法,比较类似,当然是今天临时写的。

知道了这题就完成了leetcode 4的第二重二分的写法了吧,楼主懒。。。

 class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
int l = , r = nums.size() - ;
while(l <= r){
int mid = l + (r - l)/;
if(nums[mid] < target){
l = mid + ;
}
else r = mid - ;
}
return l;
}
};

这里给出楼主当时A题的解法,请不要鄙视楼主。。。

 class Solution {
public:
int searchInsert(vector<int>& nums, int target){
vector<int>::iterator it = lower_bound(nums.begin(), nums.end(), target);
return it - nums.begin();
}
};

是的,又是库函数^_^

Leetcode 35 Search Insert Position 二分查找(二分下标)的更多相关文章

  1. LeetCode 35 Search Insert Position(查找插入位置)

    题目链接: https://leetcode.com/problems/search-insert-position/?tab=Description   在给定的有序数组中插入一个目标数字,求出插入 ...

  2. [array] leetcode - 35. Search Insert Position - Easy

    leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...

  3. [LeetCode] 35. Search Insert Position 搜索插入位置

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

  4. 【LeetCode】- Search Insert Position(查找插入的位置)

    [ 问题: ] Given a sorted array and a target value, return the index if the target is found. If not, re ...

  5. [leetcode 35] Search Insert Position

    1 题目: Given a sorted array and a target value, return the index if the target is found. If not, retu ...

  6. [LeetCode] 35. Search Insert Position 解决思路

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

  7. LeetCode 35. Search Insert Position (搜索嵌入的位置)

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

  8. Leetcode 35——Search Insert Position

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

  9. [leetcode]35. Search Insert Position寻找插入位置

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

随机推荐

  1. TCP头部结构

    3.2 TCP头部结构 TCP头部信息出现在每个TCP报文段中,用于指定通信的源端端口,目的端端口,管理TCP连接等,本节详细介绍TCP的头部结构,包括固定头部结构和头部选项. 3.2.1 TCP固定 ...

  2. css清除浮动的方法汇总

    这是在其他地方看到的一篇文章,汇总的不错,摘过来做个记录. 引用地址 : http://www.cnblogs.com/ForEvErNoME/p/3383539.html ------------- ...

  3. Ubuntu下设置(增加/删除)开机启动项

    As said above, you have to edit /etc/xdg/autostart/ and either: remove the NoDisplay=true lines; or ...

  4. jQuery中$.fn的用法

    原文:http://www.jb51.net/article/42816.htm $.fn是指jquery的命名空间,$.fn=$.prototype. 1.$.extend 为jquery添加静态方 ...

  5. sql server 基础语句

    创建数据库 创建之前判断该数据库是否存在 if exists (select * from sysdatabases where name='databaseName') drop database ...

  6. [置顶]PADS PCB功能使用技巧系列之NO.002- 如何走差分线?

    差分信号在高速电路设计中应用越来越广泛,如USB.HDMI.PCI.DDR*等,承载差分信号的差分线主要优势有:抗干扰能力强,能有效抑制EMI.时序定位精确等,对于PCB工程师来说,最关注的是如何确保 ...

  7. 工作随笔——Swift中的Range和一些字符操作

    截取字符串在Swift中相比OC要复杂很多,主要原因可能还是OC的NSRange的创建方法中参数类型为int,而Swift却对类型要求很严格,int不能作为参数创建Range,这要使用String中的 ...

  8. IIS负载均衡ARR路由请求到ARR服务器和处理服务器

    .net web 使用IIS ARR(Application Request Route)技术实现web的高性能.高可靠.易扩展及负载均衡.ARR的使用请参考 IIS负载均衡-Application ...

  9. FusionCharts简单教程(七)-----使用FusionCharts实现下钻功能

          前面介绍的FusionCharts都是对FusionCharts的基本属性进行操作,下面几篇博文就FusionCharts的高级特性做一个介绍,包括:添加下钻链接.使用Style样式定制图 ...

  10. [ACM_贪心] Radar Installation

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=28415#problem/A 题目大意:X轴为海岸线可放雷达监测目标点,告诉n个目标点和雷 ...