[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 ...
随机推荐
- 常见Z纯CSS小样式合集(三角形)
三角形 .sanjiao{ width:0px; height: 0px; overflow: hidden; border-width: 100px; border-color: transpare ...
- solr深分页,游标操作分页,解决性能问题
solr深分页,游标操作分页,解决性能问题 @Test public void pageByCursor() { try { solrServer.connect(); String query = ...
- CSS——改变浏览器滚动条样式
https://www.cnblogs.com/sun-rain/p/5789417.html
- DWG转PDF
DWG转PDF DWG转换PDF有两种方法,一种是利用PDF打印机,一种是利用专业软件: 利用PDF打印机最直接,但是不能批量打印,下面讲一下利用专业软件如何进行批量转换,在这里以梦想CAD软件(Mx ...
- Android 按钮常用点击事件大总结
很多学习Android程序设计的人都会发现每个人对代码的写法都有不同的偏好,比较明显的就是对控件响应事件的写法的不同.因此本文就把这些写法总结一下,比较下各种写法的优劣,希望对大家灵活地选择编码方式可 ...
- enote笔记语言(2)(ver0.5)
why not(whyn't) 为什么不(与“why”相反对应,是它的反面.它的矛盾对立面) how对策 how设计 key-memo: ...
- enote笔记语言(5)——其他(ver0.2)
章节:其他 ((主:单词)) 用来醒目地强调这个句子中哪个词语作主语 sentence: ...
- python3.x Day1 用户登录程序练习
训练1: 模拟登陆: 1. 用户输入帐号密码进行登陆 2. 用户信息保存在文件内 3. 用户密码输入错误三次后锁定用户 login2.py: #!/usr/bin/env python # -*- c ...
- 用python写了一个猜年龄小游戏
写一个猜年龄游戏: 需要实现用户登录的功能 初始用户登录信息为 {'hades': '13579','nick': '123','ruixing': 'a1','fanping': 'b2'} 登录时 ...
- python爬虫29 | 使用scrapy爬取糗事百科的例子,告诉你它有多厉害!
是时候给你说说 爬虫框架了 使用框架来爬取数据 会节省我们更多时间 很快就能抓取到我们想要抓取的内容 框架集合了许多操作 比如请求,数据解析,存储等等 都可以由框架完成 有些小伙伴就要问了 你他妈的 ...