题意:

  给一个升序的数组,如果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. 《鸟哥的Linux私房菜》读书笔记4

    1. grep查找 grep 'string' filename; last | grep 'root'; 以行为单位. 利用参数-i(忽略大小写),-v(反相)等进行正则表达式: ‘’中可以为正则表 ...

  2. Jade(Pug) 模板引擎使用文档

    本篇内容 在 Express 中调用 jade 模板引擎 jade 变量调用 if 判断 循环 Case 选择 在模板中调用其他语言 可重用的 jade 块 (Mixins) 模板包含 (Includ ...

  3. [Unity3D] 5.0 图集合并扩展工具,用于解决UGUI与AssetBundle打包造成资源包过大的问题

    [Unity3D] 5.0 图集合并扩展工具,用于解决UGUI与AssetBundle打包造成资源包过大的问题 2017年07月05日 15:57:44 阅读数:1494 http://www.cpp ...

  4. luogu 3806 【模板】点分治

    luogu 3806 [模板]点分治 给定一棵有n个点的树,有m个询问,每个询问树上距离为k的点对是否存在.树的权值最多不超过c.n<=10000,m<=100,c<=1000,K& ...

  5. 深入解析Android Design包——Behavior

    已经说过了,在AndroidDesign包中主要有两个核心概念:一是NestedScroll,另一个就是Behavior. 相比于NestedScroll这个概念来说,Behavior分析起来会难很多 ...

  6. PAT天梯赛L2-007 家庭房产

    题目链接:点击打开链接 给定每个人的家庭成员和其自己名下的房产,请你统计出每个家庭的人口数.人均房产面积及房产套数. 输入格式: 输入第一行给出一个正整数N(<=1000),随后N行,每行按下列 ...

  7. 2017 ACM/ICPC Asia Regional Shenyang Online transaction transaction transaction

    Problem Description Kelukin is a businessman. Every day, he travels around cities to do some busines ...

  8. Django - 权限(3)- 动态显示二级菜单

    一.动态显示二级菜单 上篇随笔中,我们实现了动态显示一级菜单,现在考虑这样一种情况,用户的菜单权限比较多,这个时候全部并列展现在左侧菜单就不合适了,所以,现在有这样一个需求,即把用户的菜单权限分类,划 ...

  9. 自定义滚动条插件 mCustomScrollbar 使用介绍

    引用有心的学士笔记 http://www.wufangbo.com/mcustomscrollbar/ http://www.jianshu.com/p/550466260856 官网地址 http: ...

  10. postgresql 导出csv格式的数据后使用excel打开中文乱码的问题

    两种方法: 1>使用excel 的自文本导入功能,具体方法: 1) 打开 Excel 2) 执行“数据”->“自文本” 3) 选择 CSV 文件,出现文本导入向导 4) 选择“分隔符号”, ...