LeetCode Search Insert Position (二分查找)
2024-09-30 21:42:29
题意:
给一个升序的数组,如果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代码
最新文章
- C阶段【01】 - C基础
- CentOS升级Python 2.6到2.7
- A -- HDU 4585 Shaolin
- Java实现猜数游戏
- (剑指Offer)面试题15:链表中倒数第k个结点
- 一级域名301重定向到www二级域名
- 宏定义&;CodeBlocks&;Glib
- js获取上传文件的绝对路径
- UVA-12166 天平性质+字符处理
- 动态规划 POJ3616 Milking Time
- 01 JVM 从入门到实战 | 什么是 JVM
- Ambari Agent 源码分析
- mac 配置 ssh 到git (Could not resolve hostname github.com, Failed to connect to github.com port 443 Operation timed out)
- OWIN 自托管静态网站
- Cmder使用ls中文显示乱码解决方案
- [Python设计模式] 第15章 如何兼容各种DB——抽象工厂模式
- 向jsp中引入公共文件
- java 泛型的通配符和限定
- ueditor上传图片设置的简单实例
- LINQ to SQL和Entity Framework
热门文章
- jquery事件之事件处理函数
- restfull知识点
- nginx 服务器并发优化
- 解读人:朱月琴,Hippocampal proteomic alteration in triple transgenic mouse model of Alzheimer’s disease and implication of PINK 1 regulation in donepezil treatment
- EOS 用户权限相关命令
- express-http-proxy 的基础使用
- 外键约束 foreign key
- 加注解时插入权限切面@EnableDataSecurity
- (转)linux 中特殊符号用法详解
- 2 - Bootstrap-引导类-Bootstrap/ServerBootstrap