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 ...
随机推荐
- ADT-23.0.2百度网盘下载地址
最近 Google 被墙 http://download.csdn.net/download/wentai2009/7736389
- sql获取当日减去几天的几天前日期
CONVERT(varchar(10),DATEADD(DAY, -220 ,CONVERT(nvarchar(10),getdate(),23)),23)
- 【java数据类型和mysqk数据类型对照表】
java mysql 数据类型对照 类型名称 显示长度 数据库类型 JAVA类型 JDBC类型索引(int) 描述 VARCHAR L+N VARCHAR java.lan ...
- 谈谈asp.net中的<% %>,<%= %>,<%# %><%$ %>的使用-转
首先我们来看一下<% %>的使用 在aspx的页面中只能使用服务器控件和一般的控件,有些时候你想在该页面写入c#代码,必须使用<% %>,然后在里面写入c#的代码,下面我们来看 ...
- 该项目中不存在目标“GatherAllFilesToPublish”
在项目发布的时候报了一个 该项目中不存在目标“GatherAllFilesToPublish” 的奇怪错误,查了之后,在报错的.csproj里面添加 <Target Name="Ga ...
- VIM命令图---可定制版
近期闲来无事,便自己用PS制作了一份VIM命令助记图,之前看到网上有类似的图片,可是有些解释感觉不太好,并有一些错误的地方,所以就自己做了一份,并且保留了PS源文件,为了方便会PS的网友可以对本图进行 ...
- Spark Executor 概述
Spark Executor 工作原理: 1. 在CoarseGrainedExecutorBackend启动时向Driver注册Executor,其实质是注册ExecutorBackend实例,和E ...
- 【Kafka】《Kafka权威指南》入门
发布与订阅消息系统 在正式讨论Apache Kafka (以下简称Kafka)之前,先来了解发布与订阅消息系统的概念, 并认识这个系统的重要性.数据(消息)的发送者(发布者)不会直接把消息发送给接收 ...
- 模型事件注意点,before_delete、after_delete、before_write、after_write、before_update、after_update、before_insert、after_insert
模型类支持before_delete.after_delete.before_write.after_write.before_update.after_update.before_insert.af ...
- zabbix对tcp状态监控
1.先编写一个获取tcp状态的脚本文件,脚本放在/usr/lib/zabbix/alertscripts/vim /usr/lib/zabbix/alertscripts/tcp_status.sh ...