题意:

  给一个升序的数组,如果target在里面存在了,返回其下标,若不存在,返回其插入后的下标。

思路:

  来一个简单的二分查找就行了,注意边界。

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

AC代码

LeetCode Search Insert Position (二分查找)的更多相关文章

  1. LeetCode Search Insert Position (二分查找)

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

  2. Leetcode 35 Search Insert Position 二分查找(二分下标)

    基础题之一,是混迹于各种难题的基础,有时会在小公司的大题见到,但更多的是见于选择题... 题意:在一个有序数列中,要插入数target,找出插入的位置. 楼主在这里更新了<二分查找综述>第 ...

  3. 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,Search for a Range (二分查找,lower_bound,upper_bound)

    Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...

  5. LeetCode: Search Insert Position 解题报告

    Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...

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

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

  7. 【LeetCode每天一题】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】- Search Insert Position(查找插入的位置)

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

  9. [Leetcode] search insert position 寻找插入位置

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

随机推荐

  1. Cygwin install apt-cyg

    1. UPDATE CYGWIN First of all you will need to ensure that Cygwin has the necessary binaries require ...

  2. 机器学习--近邻成分分析(NCA)算法 和 度量学习

    1.近邻成分分析(NCA)算法 以上内容转载自:http://blog.csdn.net/chlele0105/article/details/13006443 2.度量学习 在机器学习中,对高维数据 ...

  3. Good Bye 2014 B. New Year Permutation(floyd )

    题目链接 题意:给n个数,要求这n个数字小的尽量放到前面,求一个最小的. 给一个矩阵s[i][j]==1,表示位置 i 的数字可以和 位置 j 的数字交换. 分析: 刚开始用的是3个循环,每次都找一个 ...

  4. cookie 单值设置

  5. redis命令集

    查看使用运行服务:ping 关闭服务的连接:quit 切换数据库:select index 连接: redis-cli -h -a myPassword 查看密码: config get requir ...

  6. Hadoop localhost ssh 免密码登陆

    配置本地ssh免密码登陆,遇到很奇怪的问题,原来在公司电脑上,是按照 http://blog.csdn.net/hackerwin7/article/details/28109073 这里说的配置的, ...

  7. 洛谷P1034 矩形覆盖

    P1034 矩形覆盖 题目描述 在平面上有 n 个点(n <= 50),每个点用一对整数坐标表示.例如:当 n=4 时,4个点的坐标分另为:p1(1,1),p2(2,2),p3(3,6),P4( ...

  8. 解决LINUX下SQLPLUS时上下左右键乱码问题

    window下的sqlplus可以通过箭头键,来回看历史命令,用起来非常的方便. 但是在linux里就没有这么方面了,错了一个命令,我们必须重新敲一次,辛苦了手指头叻. 看到一个文章,很方便的一招,给 ...

  9. 自制Java虚拟机(四)-对象、new、invokespecial

    自制Java虚拟机(四)-对象.new.invokespecial 一.对象的表示 刚开始学Java的时候,图书馆各种教程,书名往往都是“Java面向对象高级编程”,通常作者都会与C++做个比较,列出 ...

  10. 高效法则 之 你还在用这么low的方法打开软件吗?

    电脑上的诸多功能被组织成软件的形式提供给用户.聊天的有QQ/Skype,看电影的有腾讯视频/芒果TV,听音乐的有酷狗/LessDJ,做图的有PS/Sketch,写代码搞开发的各种IDE Eclipse ...