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 ...
随机推荐
- 如何保持blog的高质量(相对于自己的进步而言的)
多写! 多改!! 多删!!!
- 获取app下载链接
https://itunes.apple.com/cn/app/id1398635899?mt=8 只需要更改其中的id就可以了
- .net core 深入了解配置文件加载过程
前言 配置文件中程序运行中,担当着不可或缺的角色:通常情况下,使用 visual studio 进行创建项目过程中,项目配置文件会自动生成在项目根目录下,如 appsettings.json, ...
- C++的STL总结(2)
紧接着上篇博客,把没总结全的继续补充. (13)set容器 set是用红黑树的平衡二叉索引树的数据结构来实现的,插入时,它会自动调节二叉树排列,把元素放到适合的位置,确保每个子树根节点的键值大于左子树 ...
- python字符串和日期相互转换
- Mice and Holes 单调队列优化dp
Mice and Holes 单调队列优化dp n个老鼠,m个洞,告诉你他们的一维坐标和m个洞的容量限制,问最小总距离.1 ≤ n, m ≤ 5000. 首先列出朴素的dp方程:\(f[i][j] ...
- springBoot2.0 配置shiro实现权限管理
一.前言 基于上一篇springBoot2.0 配置 mybatis+mybatisPlus+redis 这一篇加入shiro实现权限管理 二.shiro介绍 2.1 功能特点 Shiro 包含 10 ...
- Python网络编程(一)
最近在啃<python核心编程(第三版)>,感觉这本书并不是特别的友好,虽然有基于python3提出的改进代码:但是整书的基准感觉还是在python2.7.所以python3的代码中还是有 ...
- Knight Tournament (set)
Hooray! Berl II, the king of Berland is making a knight tournament. The king has already sent the me ...
- 【aspnetcore】配置使用jwt验证
因为害怕token泄露出现问题,所以从未在项目中使用jwt.但这玩意现在真的很火,趁有空还是研究了一下. 在aspnetcore中实现jwt很简单,感觉微软把很多工作都做了,虽然开发效率上去了,但是使 ...