[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 ...
随机推荐
- 预采订单管理接收来源App数据
- Modbus通讯协议简介
Modbus协议简介 Modbus协议最初由Modicon公司开发出来,此协议支持传统的RS-232.RS-422.RS-485和以太网设备,许多工业设备,包括PLC,DCS,智能仪表等都在使用Mod ...
- C语言指针的理解以及指针的指针的理解
指针指向的是内存地址编号,内存地址编号指向的是对应的内容. 我们需要一个变量,来储存内存地址编号,这个变量的值是一个内存地址编号,但是我们可以通过修改变量的值,来不断的改变内存地址编号. 但是,我们如 ...
- 【ZT】在微信上有哪些高情商的说话方式 | M周刊(听语音需要60秒,看文字只需10秒)
https://baijiahao.baidu.com/s?id=1590547145580792217&wfr=spider&for=pc https://yuedu.baidu.c ...
- mvc使用linq to sql进行sum统计遇到查询为null的问题
mvc linq to sql,linq to entity,sum,null 昨天写了段sum的统计语句, decimal sums sums = ( from fac in db.Apply wh ...
- iOS UI布局总结
布局就是尺寸和位置的设置. 一.基本布局: 1)绝对布局:frame.layoutsubviews. 二.相对布局: autoresizing.autolayout.基于父视图.基于约束. 三.线性布 ...
- redis的安装和使用【2】redis的java操作
修改redis.conf# 配置绑定ip,作者机子为192.168.100.192,请读者根据实际情况设置bind 192.168.100.192#非保护模式protected-mode no保存重启 ...
- C++调用Com
需求:1.创建myCom.dll,该COM只有一个组件,两个接口: IGetRes--方法Hello(), IGetResEx--方法HelloEx() 2.在工程中导入组件或类型库 #im ...
- Nginx 通过 certbot 为网站自动配置 SSL 证书并续期
一.背景知识 1.1.http 和 https 是什么? 简单来说,http 是一个传输网页内容的协议,比如你看到的 http 开头的网站 http://www.163.com ,其网页上的文字.图片 ...
- 解决window.location.href参数太长
前言:一提到页面跳转,最常用的一般就是window.location.href,如果需要带参数,也许可以在后面用?拼上,但这样并不安全,而且有个更严重的问题,这样的拼接是有长度限制的,如果达到好几千个 ...