[LeetCode] 035. Search Insert Position (Medium) (C++)
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql)
Github: https://github.com/illuz/leetcode
035. Search Insert Position (Medium)
链接:
题目:https://leetcode.com/problems/search-insert-position/
代码(github):https://github.com/illuz/leetcode
题意:
要把一个数有序插入到一个有序数组里,问插入的位置。
分析:
还是二分变形题。
- 用 STL 的
lower_bound
偷懒。 - 二分,最后注意推断一下是否找到。要输出什么。
代码:
C++:
class Solution {
public:
int searchInsert(int A[], int n, int target) {
// if use lower_bound
// return lower_bound(A, A + n, target) - A; int l = 0, r = n - 1, mid = 0;
while (l < r) {
mid = l + (r - l) / 2;
// mid = (l + r) / 2;
if (A[mid] < target)
l = mid + 1;
else
r = mid;
}
return A[l] < target ? l + 1 : l;
}
};
[LeetCode] 035. Search Insert Position (Medium) (C++)的更多相关文章
- LeetCode 035 Search Insert Position
题目要求:Search Insert Position Given a sorted array and a target value, return the index if the target ...
- Java for LeetCode 035 Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- [array] leetcode - 35. Search Insert Position - Easy
leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...
- [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 ...
- [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 ...
- Leetcode 35 Search Insert Position 二分查找(二分下标)
基础题之一,是混迹于各种难题的基础,有时会在小公司的大题见到,但更多的是见于选择题... 题意:在一个有序数列中,要插入数target,找出插入的位置. 楼主在这里更新了<二分查找综述>第 ...
- 【题解】【数组】【查找】【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] 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 35. Search Insert Position (搜索嵌入的位置)
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
随机推荐
- [ TJOI 2007 ] 线段
\(\\\) \(Description\) 一个\(N\times N\) 的网格,每行有一段要必走,求从\((1,1)\)到\((N,N)\)的最短路长度. \(N\le 2\times10^4\ ...
- 循环插入记录,id每次加1
sql语句写法: begin for i in 1 .. 100 loop insert into table_name values(....); end loop; commit; end; 例子 ...
- Codeforces_750_C_(二分查找)
C. New Year and Rating time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- Jmeter之JDBC请求参数化(一)
一.环境准备 a.jmeter5.1.1版本最新版本,可以去网页下载:https://jmeter.apache.org/download_jmeter.cgi b.jdbc驱动:链接:https:/ ...
- RabbitMQ系列(一)--消息中间件MQ如何去选择
MQ在项目中的应用很普遍,本人所在项目组使用的是ActiveMQ,但是后面介绍的RabbitMQ... 一.应用场景 1.异步处理 2.流量削峰.秒杀 3.日志处理,推荐kafka 4.应用解耦 二. ...
- 解决移动端 手机号input 属性为 number,maxlength无效情况
<input type="number" oninput="if(value.length>11)value=value.slice(0,11)" ...
- JDK的下载---官方
1.去到官方网站 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 根据箭头选择, ...
- java基数排序
代码如下: import java.util.Arrays; public class MultiKeyRadixSort { public static void radixSort(int [] ...
- 验证DNS解析失败:解决办法之一
今天晚上练习简单的DNS解析验证: 环境是在一台虚拟机上搭建,另一台虚拟机验证,步骤如下: 虚拟机A: 1.安装软件包 bind 和bind-chroot[root@svr7 ~]# yum -y ...
- python3支持excel读写
1.安装setuptools-17.0.tar.gz cmd 进入命令行 cd C:\Users\vivi\Desktop\pythonforexcel\setuptools-17.0\setupto ...