首页
题库
面试
求职
学习
竞赛
More+
所有博客
搜索面经/职位/试题/公司
搜索
我要招人
去企业版
登录 / 注册
首页
>
试题广场
>
插入区间
[编程题]插入区间
热度指数:1741
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 256M,其他语言512M
算法知识视频讲解
给定一个无重叠的,按照区间起点升序排列的区间列表,在列表中插入一个新区间,如果有原区间有重合,则合并,请返回插入后的区间列表。
数据范围:区间列表长度满足
, 区间的左右端点满足
示例1
输入
[[2,5],[6,11]],[5,6]
输出
[[2,11]]
示例2
输入
[],[2,5]
输出
[[2,5]]
马上挑战
算法知识视频讲解
提交运行
算法知识视频讲解
添加笔记
求解答(0)
邀请回答
收藏(17)
分享
纠错
提交结果有问题?
8个回答
6篇题解
开通博客
用户抉择
发表于 2022-06-18 23:04:31
/** * struct Interval { * int start; * int end; * Interval(int s, int e) : s
展开全文
空中转体一周半
发表于 2022-05-22 09:51:54
这个题看起来可以使用“合并区间”的解法,但题目已经告知了输入的数组是已经按start字段升序,那么我们仅需要找到插入点,然后向后合并即可。时间复杂度O(n),空间复杂度O(n)(采用了list)。 public class Solution { public Interval[] inser
展开全文
你说夕阳很美
发表于 2022-01-08 17:56:46
/** * struct Interval { * int start; * int end; * Interval(int s, int e) : start(start), end(e) {} * }; */ class Solution { public: /**
展开全文
姐姐的遮阳伞
发表于 2022-03-24 20:26:57
import java.util.*; /* * public class Interval { * int start; * int end; * public Interval(int start, int end) { * this.start = start;
展开全文
fred-coder
发表于 2022-01-02 15:45:10
贪心,将 newInterval 加入到 Intervals 然后对其排序,用栈做存储;依次遍历 Intervals,对比栈顶元素,最终返回栈 # class Interval: # def __init__(self, a=0, b=0): # self.start = a
展开全文
牛客362459853号
发表于 2021-11-24 20:49:54
描述 给定一个无重叠的,按照区间起点升序排列的区间列表,在列表中插入一个新区间,如果有原区间有重合,则合并,请返回插入后的区间列表。 数据范围:区间列表长度满足 , 区间的左右端点满足 问题分析:插入区间,无非就是对要插入的区间
展开全文
问题信息
数组
难度:
8条回答
17收藏
1031浏览
热门推荐
通过挑战的用户
查看代码
牛客84040...
2022-09-16 01:43:42
ariou
2022-09-15 21:13:57
tq要上榜
2022-09-15 15:44:50
firefly...
2022-09-14 08:37:49
Asphyxiay
2022-09-14 01:11:18
相关试题
设某二叉树的先序遍历序列为abdg...
树
评论
(1)
游戏内数据分析涉猎的少,如何证明自...
评论
(1)
之前的经历中单品数据分析的经验丰富...
评论
(1)
什么样的人适合做数据分析
评论
(1)
2022 诺瓦科技 Perl re...
perl
System Verilog
评论
(1)
扫描二维码,关注牛客网
意见反馈
下载牛客APP,随时随地刷题
import java.util.*; /* * public class Interval { * int start; * int end; * public Interval(int start, int end) { * this.start = start; * this.end = end; * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param Intervals Interval类一维数组 * @param newInterval Interval类 * @return Interval类一维数组 */ public Interval[] insertInterval (Interval[] Intervals, Interval newInterval) { // write code here } }
/** * struct Interval { * int start; * int end; * Interval(int s, int e) : start(start), end(e) {} * }; */ class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param Intervals Interval类vector * @param newInterval Interval类 * @return Interval类vector */ vector
insertInterval(vector
& Intervals, Interval newInterval) { // write code here } };
#coding:utf-8 # class Interval: # def __init__(self, a=0, b=0): # self.start = a # self.end = b # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param Intervals Interval类一维数组 # @param newInterval Interval类 # @return Interval类一维数组 # class Solution: def insertInterval(self , Intervals , newInterval ): # write code here
using System; using System.Collections.Generic; /* public class Interval { public int start; public int end; public Interval () { start = 0; end = 0; } public Interval (int s, int e) { start = s; end = e; } } */ class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param Intervals Interval类一维数组 * @param newInterval Interval类 * @return Interval类一维数组 */ public List
insertInterval (List
Intervals, Interval newInterval) { // write code here } }
/* * function Interval(a, b){ * this.start = a || 0; * this.end = b || 0; * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param Intervals Interval类一维数组 * @param newInterval Interval类 * @return Interval类一维数组 */ function insertInterval( Intervals , newInterval ) { // write code here } module.exports = { insertInterval : insertInterval };
start = $a; $this->end = $b; } }*/ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param Intervals Interval类一维数组 * @param newInterval Interval类 * @return Interval类一维数组 */ function insertInterval( $Intervals , $newInterval ) { // write code here }
# class Interval: # def __init__(self, a=0, b=0): # self.start = a # self.end = b # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param Intervals Interval类一维数组 # @param newInterval Interval类 # @return Interval类一维数组 # class Solution: def insertInterval(self , Intervals: List[Interval], newInterval: Interval) -> List[Interval]: # write code here
package main import "fmt" import . "nc_tools" /* * type Interval struct { * Start int * End int * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param Intervals Interval类一维数组 * @param newInterval Interval类 * @return Interval类一维数组 */ func insertInterval( Intervals []*Interval , newInterval *Interval ) []*Interval { // write code here }
/** * struct Interval { * int start; * int end; * }; */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param Intervals Interval类一维数组 * @param IntervalsLen int Intervals数组长度 * @param newInterval Interval类 * @return Interval类一维数组 * @return int* returnSize 返回数组行数 */ struct Interval* insertInterval(struct Interval* Intervals, int IntervalsLen, struct Interval newInterval, int* returnSize ) { // write code here }
# class Interval # attr_accessor :start, :end # # def initialize(a = 0, b = 0) # @start, @end = a, b # end # end # # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param Intervals Interval类一维数组 # @param newInterval Interval类 # @return Interval类一维数组 # class Solution def insertInterval(Intervals, newInterval) # write code here end end
/** * class Interval(var start: Int, var end: Int) */ object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param Intervals Interval类一维数组 * @param newInterval Interval类 * @return Interval类一维数组 */ def insertInterval(Intervals: Array[Interval],newInterval: Interval): Array[Interval] = { // write code here } }
/** * class Interval(var start: Int, var end: Int) */ object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param Intervals Interval类一维数组 * @param newInterval Interval类 * @return Interval类一维数组 */ fun insertInterval(Intervals: Array
,newInterval: Interval?): Array
{ // write code here } }
import java.util.*; /* * public class Interval { * int start; * int end; * public Interval(int start, int end) { * this.start = start; * this.end = end; * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param Intervals Interval类一维数组 * @param newInterval Interval类 * @return Interval类一维数组 */ public Interval[] insertInterval (Interval[] Intervals, Interval newInterval) { // write code here } }
/*class Interval { * start: number * end: number * constructor(start: number, end: number) { * this.start = start * this.end = end * } * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param Intervals Interval类一维数组 * @param newInterval Interval类 * @return Interval类一维数组 */ export function insertInterval(Intervals: Interval[], newInterval: Interval): Interval[] { // write code here }
/** * public class Interval { * public var start: Int * public var end: Int * public init(_ start: Int = 0, _ end: Int = 0) { * self.start = start * self.end = end * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param Intervals Interval类一维数组 * @param newInterval Interval类 * @return Interval类一维数组 */ func insertInterval ( _ Intervals: [Interval], _ newInterval: Interval) -> [Interval] { // write code here } }
/** * #[derive(PartialEq, Eq, Clone, Debug)] * pub struct Interval { * pub start: i32, * pub end: i32, * } * * impl Interval { * #[inline] * fn new(start: i32, end: i32) -> Self { * Interval { * start: start, * end: end, * } * } * } */ struct Solution{ } impl Solution { fn new() -> Self { Solution{} } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param Intervals Interval类一维数组 * @param newInterval Interval类 * @return Interval类一维数组 */ pub fn insertInterval(&self, Intervals: Vec
, newInterval: Interval) -> Vec
{ // write code here } }
[[2,5],[6,11]],[5,6]
[[2,11]]
[],[2,5]
[[2,5]]