57. Insert Interval
description:
插入新的区间,涉及到区间的合并.
Note:
Example:
Example 1:Input: intervals = [[1,3],[6,9]], newInterval = [2,5]
Output: [[1,5],[6,9]]Example 2:Input: intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]
Output: [[1,2],[3,10],[12,16]]
Explanation: Because the new interval [4,8] overlaps with [3,5],[6,7],[8,10].
answer:
class Solution {
public:vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval) {int n = intervals.size(), cur = 0;vector<vector<int>> res;while (cur < n && intervals[cur][1] < newInterval[0]) {res.push_back(intervals[cur++]);}while (cur < n && intervals[cur][0] <= newInterval[1]) {newInterval[0] = min(newInterval[0], intervals[cur][0]);newInterval[1] = max(newInterval[1], intervals[cur][1]);++cur;}res.push_back(newInterval);while (cur < n) {res.push_back(intervals[cur++]);}return res;}
};
relative point get√:
hint :
第一个循环把不和给定新区间重叠的,应该放在新区间前面的区间先放在答案里
第二个循环处理有重叠的部分,一直到后边最后一个没有重叠了退出
最后一个循环将新区间后边的没有重叠的放到答案里
转载于:https://www.cnblogs.com/forPrometheus-jun/p/11285743.html
57. Insert Interval相关推荐
- 【细节实现题】LeetCode 57. Insert Interval
LeetCode 57. Insert Interval Solution1:我的答案 算法很简单,不知道为何会被标记为hard题目 /*** Definition for an interval.* ...
- Insert Interval
Insert Interval 题解 题目描述 即向有序.不重叠的区间序列中插入一个区间.如区间产生重叠,则合并.求插入新区间后的区间序列. 如:A = [1,3],[6,9],插入[2,6],插入后 ...
- 057 Insert Interval 插入区间
给出一个无重叠的按照区间起始端点排序的区间列表. 在列表中插入一个新的区间,你要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间). 示例 1: 给定区间 [1,3],[6,9],插入并 ...
- LeetCode57 Insert Interval
题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nec ...
- 合并区间 · Merge Intervals 插入区间 · Insert Interval
[抄题]: 给出若干闭合区间,合并所有重叠的部分. 给出的区间列表 => 合并后的区间列表: [ [[1, 3], [1, 6],[2, 6], => [8, 10],[8, 10], [ ...
- LeetCode刷题指南(一)
以下是我个人做题过程中的一些体会: 1. LeetCode的题库越来越大,截止到目前,已经有321个问题了.对于大多数人来说,没有时间也没有必要把所有题目都做一遍(时间充裕可以随意).刷个100题左右 ...
- Leetcode重点250题
LeetCode重点250题 这个重点题目是把LeetCode前400题进行精简.精简方法如下: 删除不常考,面试低频出现题目 删除重复代码题目(例:链表反转206题,代码在234题出现过) 删除过于 ...
- Leetcode刷题指南和top100题目
原文链接:https://blog.csdn.net/qq_39521554/article/details/79160815 参考博文:https://blog.csdn.net/mmc2015/a ...
- 【LeetCode题解】排序
1. 排序 排序(sort)是一种常见的算法,把数据根据特定的顺序进行排列.经典的排序算法如下: 冒泡排序(bubble sort) 插入排序(insertion sort) 选择排序(selecti ...
最新文章
- C功底挑战Java菜鸟入门概念干货(一)
- VIM入门必读(转)
- 恒生电子linux笔试题,恒生电子一面
- 你问我答,准备面试需要做哪些技术储备,面试官更加关心什么方面的技术点?...
- Spring Bean初始化过程
- Android应用【世界杯知识答题】的界面设计经验
- 【java笔记】转换流
- 宝塔面板如何部署Java项目教程【新版】
- Python【每日一问】27
- 【Go语言】动态库和静态库详解
- 精益生产管理专家——安岷老师
- 免费ftp服务器软件,实用的3款免费ftp服务器软件
- mb是做1还是0_新手爸妈看过来:0-1岁宝宝这样做早教,省钱省心又实用
- 程序分析-Joern工具工作流程分析
- iphone4S 怎么激活?
- 在mac的m1版本安装nginx
- android 模拟自动点击,自动点击模拟器app
- caffe的安装配置(CUDA7.5+VS2013)
- aws s3 参与s3game寻找宝藏游戏挑战学习s3对象存储
- Java程序:如何将句子(字符串)中的字母大小写转化