LeetCode Search Insert Position (二分查找)
题意:
给一个升序的数组,如果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 (二分查找)的更多相关文章
- LeetCode Search Insert Position (二分查找)
题意 Given a sorted array and a target value, return the index if the target is found. If not, return ...
- Leetcode 35 Search Insert Position 二分查找(二分下标)
基础题之一,是混迹于各种难题的基础,有时会在小公司的大题见到,但更多的是见于选择题... 题意:在一个有序数列中,要插入数target,找出插入的位置. 楼主在这里更新了<二分查找综述>第 ...
- 35. Search Insert Position(二分查找)
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- 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 ...
- LeetCode: Search Insert Position 解题报告
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- LeetCode 35 Search Insert Position(查找插入位置)
题目链接: https://leetcode.com/problems/search-insert-position/?tab=Description 在给定的有序数组中插入一个目标数字,求出插入 ...
- 【LeetCode每天一题】Search Insert Position(搜索查找位置)
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- 【LeetCode】- Search Insert Position(查找插入的位置)
[ 问题: ] Given a sorted array and a target value, return the index if the target is found. If not, re ...
- [Leetcode] search insert position 寻找插入位置
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
随机推荐
- Ubuntu 安装 samba 实现文件共享和source insight 阅读uboot
环境:win10 + 虚拟机Ubuntu 12.04 一. samba的安装: # sudo apt-get install samba # sudo apt-get install smbfs 二. ...
- 【MYSQL】删除数据后自动增长列归0的问题
在清空数据表后发现自动增长id列在新增数据后仍然会按照之前的顺序生成 强迫症,就是想清空数据后让id从0开始,于是百度 执行以下sql语句可以让自动增长列归0 truncate table 表名 这是 ...
- sql server 中raiserror的使用
server数据库中raiserror的作用就和asp.net中的throw new Exception一样,用于抛出一个异常或错误.这个错误可以被程序捕捉到. raiserror('错误的描述',错 ...
- 清北刷题冲刺 11-01 a.m
立方体 /* 输入数据中的p的位置是没有用的,而题目本质上是求C(n,k) */ #include<iostream> #include<cstdio> #define mod ...
- 通过sql语句对MySql数据库的基本操作
一.数据库的基本操作 CREATE DATABASE mybookstore; DROP DATABASE mybookstore; 二.表的基本操作 1.创建表 insert into 表名(字段名 ...
- Pycharm自动部署项目
Pycharm自动部署项目 大家好呀,又有几天不见各位了.断更了几天,给大家说声抱歉.清明节大家都挺忙的,有扫墓祭祖的,也有趁小长假去游玩的. 所以,在节后,更新也会照常进行,继续给大家分享本人的一些 ...
- iOS获取手机型号、iOS获取当前app的名称和版本号
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary]; CFShow(infoDictionary); // ap ...
- tomcat memecached session 共享同步问题的解决
事件缘由:一个主项目“图说美物”,另外一个子功能是品牌商的入驻功能,是跟主项目分开的项目,为了共享登录的用户信息,而实现session共享,俩个tomcat,一个tomcat6,一个tomcat7 w ...
- Linux如何用查看域名解析
方法/步骤 查看本地dns配置.确保能上网,dns配置正确.可以查看网卡配置文件和dns配置文件,网卡里配置优先. ping命令.第一行会返回域名及解析的ip. host命令.会返回域 ...
- 关于String的split方法
在做剑指offer的时候,有一道替换空格的题,立刻就想到用这个split方法来做,但发现,这个方法会丢掉字符串最后的空格??? 百度后,知道原因,这里直接复制粘贴了: 在使用java中的split按照 ...