[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 index where it would be if it were inserted in order.
You may assume no duplicates in the array.
Example 1:
Input: [1,3,5,6], 5
Output: 2
Example 2:
Input: [1,3,5,6], 2
Output: 1
Example 3:
Input: [1,3,5,6], 7
Output: 4
Example 4:
Input: [1,3,5,6], 0
Output: 0
这道题基本没有什么难度,实在不理解为啥还是 Medium 难度的,完完全全的应该是 Easy 啊(貌似现在已经改为 Easy 类了),三行代码搞定的题,只需要遍历一遍原数组,若当前数字大于或等于目标值,则返回当前坐标,如果遍历结束了,说明目标值比数组中任何一个数都要大,则返回数组长度n即可,代码如下:
解法一:
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
for (int i = ; i < nums.size(); ++i) {
if (nums[i] >= target) return i;
}
return nums.size();
}
};
当然,我们还可以用二分搜索法来优化时间复杂度,而且个人认为这种方法应该是面试官们想要考察的算法吧,属于博主之前的总结帖 LeetCode Binary Search Summary 二分搜索法小结 中第二类 - 查找不小于目标值的数,参见代码如下:
解法二:
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
if (nums.back() < target) return nums.size();
int left = , right = nums.size();
while (left < right) {
int mid = left + (right - left) / ;
if (nums[mid] < target) left = mid + ;
else right = mid;
}
return right;
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/35
类似题目:
参考资料:
https://leetcode.com/problems/search-insert-position/
https://leetcode.com/problems/search-insert-position/discuss/15372/Simple-Java-solution
https://leetcode.com/problems/search-insert-position/discuss/15080/My-8-line-Java-solution
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] 35. Search Insert Position 搜索插入位置的更多相关文章
- [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】Search Insert Position(搜索插入位置)
这道题是LeetCode里的第35道题. 题目描述: 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元 ...
- [array] leetcode - 35. Search Insert Position - Easy
leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...
- [LeetCode] Search Insert Position 搜索插入位置
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- lintcode:Search Insert Position 搜索插入位置
题目: 搜索插入位置 给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引.如果没有,返回到它将会被按顺序插入的位置. 你可以假设在数组中无重复元素. 样例 [1,3,5,6],5 → 2 ...
- LeetCode 35 Search Insert Position(查找插入位置)
题目链接: https://leetcode.com/problems/search-insert-position/?tab=Description 在给定的有序数组中插入一个目标数字,求出插入 ...
- 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 ...
- [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 ...
随机推荐
- 你需要知道的8个CSS带@的规则
1:@charset(用来设置html文档字符编码的格式,比如我们常用的,charset='utf-8') 注:建议CSS文件最顶部都加上@charset "utf-8";,避免出 ...
- redis之线程IO模型
非阻塞 IO 当我们调用套接字的读写方法,默认它们是阻塞的,比如 read 方法要传递进去一个参数n,表示读取这么多字节后再返回,如果没有读够线程就会卡在那里,直到新的数据到来或者连接关闭了,read ...
- POJ-2006 Litmus Test 高精度
The pH scale measures the concentration of protons (H +) in a solution and, therefore, its acidity o ...
- 基于log4net的日志组件扩展封装,实现自动记录交互日志 XYH.Log4Net.Extend(微服务监控)
背景: 随着公司的项目不断的完善,功能越来越复杂,服务也越来越多(微服务),公司迫切需要对整个系统的每一个程序的运行情况进行监控,并且能够实现对自动记录不同服务间的程序调用的交互日志,以及通一个服务或 ...
- Java生鲜电商平台-定时器,定时任务quartz的设计与架构
Java生鲜电商平台-定时器,定时任务quartz的设计与架构 说明:任何业务有时候需要系统在某个定点的时刻执行某些任务,比如:凌晨2点统计昨天的报表,早上6点抽取用户下单的佣金. 对于Java开源生 ...
- 数据库系统(五)---MySQL基础
一.SQL基本概念: SQL 已经成为关系数据库的标准语言,是一种数据库查询和程序设计语言,用 于存取数据以及查询.更新和管理关系数据库系统. 功能不仅仅是查询,还包括数据定义.数据操纵和数据控制等于 ...
- 10.Redis 主从架构
作者:中华石杉 Redis 主从架构 单机的 redis,能够承载的 QPS 大概就在上万到几万不等.对于缓存来说,一般都是用来支撑读高并发的.因此架构做成主从(master-slave)架构,一主多 ...
- Django 练习班级管理系统六 -- 编辑老师列表
修改 views.py @auth def edit_teacher(request, nid): if request.method == "GET": obj = models ...
- Alpha版本1发布
这个作业属于哪个课程 课程的链接 这个作业的要求在哪里 作业要求的链接 团队名称 西柚三剑客 这个作业的目标 -Alpha版本发布说明 1. 团队成员的学号列表 团队成员 学号 whisperzxx( ...
- Ubuntu下搭建Kubernetes集群(1)--安装docker
可以使用物理机,也可以使用虚拟机. 首先参考https://docs.docker.com/install/linux/docker-ce/ubuntu/ 官方文档学会安装docker. 1.首先移除 ...