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

题意:在一个有序数列中,要插入数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. 使用html2canvas实现网页截图并嵌入到PDF

    以前我们只能通过截图工具进行截取图像.这使得在业务生产中,变得越来越不方便.目前的浏览器功能越来越强大,H5也逐渐普及,浏览器也可以实现截图了.这里来聊下之前在工作中用到的html2canvas.这里 ...

  2. JQuery延时操作

    JQuery通过setTimeout函数可以实现延时操作以完成在编程达到某些需要的效果. 使用方法如下: function doSomething() { alert("hello worl ...

  3. CrossApp入门简介

    来自9miao社区的CrossApp号称可以极大的简化移动应用的开发,因为开源的时间不长,有效的资料不多. 官方网站: http://crossapp.9miao.com/ 环境搭建说明:http:/ ...

  4. css+js回到顶部

    .backToTop { display: none; width: 18px; line-height: 1.2; padding: 5px 0; background-color: #000; c ...

  5. Bootstrap 导航栏和登陆框

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  6. day12---python mysql pymsql sqlalchemy ORM

    RDBMS 术语 在我们开始学习MySQL 数据库前,让我们先了解下RDBMS的一些术语: 数据库: 数据库是一些关联表的集合.. 数据表: 表是数据的矩阵.在一个数据库中的表看起来像一个简单的电子表 ...

  7. 解决ViewPage中嵌套有ListView或者滑动手势等造成滑动的冲突

    public class ViewPagerCompat extends ViewPager { //mViewTouchMode表示ViewPager是否全权控制滑动事件,默认为false,即不控制 ...

  8. SQLite无法使用drop column删除表字段解决办法

    由于项目需求变更,我需要在sqlite数据库的表中删除一个字段,通用的sql操作语句如下: alter table record drop column name; 结果数据库提示如下错误: 搜索得知 ...

  9. LINUX btmp 日志(lastb 命令)

    Linux下/var/log/btmp文件: 今天查看了一下服务器,发现/var/log/btmp日志文件比较大,搜索一下,此文件是记录错误登录的日志,就是说有很多人试图使用密码字典登录ssh服务,此 ...

  10. linux shell 字符串操作

    转:http://justcoding.iteye.com/blog/1963463 在做shell批处理程序时候,经常会涉及到字符串相关操作.有很多命令语句,如:awk,sed都可以做字符串各种操作 ...