[LeetCode]57.Insert Interval

2024-02-19 01:08
文章标签 leetcode interval insert 57

本文主要是介绍[LeetCode]57.Insert Interval,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).

You may assume that the intervals were initially sorted according to their start times.

Example 1:
Given intervals [1,3],[6,9], insert and merge [2,5] in as [1,5],[6,9].

Example 2:
Given [1,2],[3,5],[6,7],[8,10],[12,16], insert and merge [4,9] in as [1,2],[3,10],[12,16].

This is because the new interval [4,9] overlaps with [3,5],[6,7],[8,10].

思路

跟[LeetCode]56.Merge Intervals思路差不多。

代码

/*---------------------------------------------------------
*   日期:2015-04-22
*   作者:SJF0115
*   题目: 57.Insert Interval
*   网址:https://leetcode.com/problems/insert-interval/
*   结果:AC
*   来源:LeetCode
*   博客:
------------------------------------------------------------*/
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;struct Interval {int start;int end;Interval() : start(0), end(0) {}Interval(int s, int e) : start(s), end(e) {}
};class Solution {
public:vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {vector<Interval> result;result.push_back(newInterval);int size = intervals.size();if(size <= 0){return result;}//if// 插入for(int i = 0;i < size;++i){Interval one = result.back();Interval two = intervals[i];// [1,3] [5,6]// 插入倒数第二位if(two.end < one.start){Interval tmp = result.back();result.pop_back();result.push_back(two);result.push_back(tmp);}//if// 插入最后一位else if(two.start > one.end){result.push_back(two);}//else// 需合并else{int start = min(one.start,two.start);int end = max(one.end,two.end);Interval tmp(start,end);result.pop_back();result.push_back(tmp);}//else}//forreturn result;}
};int main(){Solution solution;Interval in1(1,2);Interval in2(3,5);Interval in3(6,7);Interval in4(8,10);Interval in5(12,16);vector<Interval> vec;vec.push_back(in1);vec.push_back(in2);vec.push_back(in3);vec.push_back(in4);vec.push_back(in5);Interval newInterval(4,9);// 合并vector<Interval> v = solution.insert(vec,newInterval);// 输出for(int i = 0;i < v.size();i++){Interval in = v[i];cout<<"["<<in.start<<","<<in.end<<"]"<<endl;}//forreturn 0;
}

运行时间

这里写图片描述

这篇关于[LeetCode]57.Insert Interval的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/723027

相关文章

mybatis执行insert返回id实现详解

《mybatis执行insert返回id实现详解》MyBatis插入操作默认返回受影响行数,需通过useGeneratedKeys+keyProperty或selectKey获取主键ID,确保主键为自... 目录 两种方式获取自增 ID:1. ​​useGeneratedKeys+keyProperty(推

Oracle 数据库数据操作如何精通 INSERT, UPDATE, DELETE

《Oracle数据库数据操作如何精通INSERT,UPDATE,DELETE》在Oracle数据库中,对表内数据进行增加、修改和删除操作是通过数据操作语言来完成的,下面给大家介绍Oracle数... 目录思维导图一、插入数据 (INSERT)1.1 插入单行数据,指定所有列的值语法:1.2 插入单行数据,指

mysql中insert into的基本用法和一些示例

《mysql中insertinto的基本用法和一些示例》INSERTINTO用于向MySQL表插入新行,支持单行/多行及部分列插入,下面给大家介绍mysql中insertinto的基本用法和一些示例... 目录基本语法插入单行数据插入多行数据插入部分列的数据插入默认值注意事项在mysql中,INSERT I

MySQL INSERT语句实现当记录不存在时插入的几种方法

《MySQLINSERT语句实现当记录不存在时插入的几种方法》MySQL的INSERT语句是用于向数据库表中插入新记录的关键命令,下面:本文主要介绍MySQLINSERT语句实现当记录不存在时... 目录使用 INSERT IGNORE使用 ON DUPLICATE KEY UPDATE使用 REPLACE

哈希leetcode-1

目录 1前言 2.例题  2.1两数之和 2.2判断是否互为字符重排 2.3存在重复元素1 2.4存在重复元素2 2.5字母异位词分组 1前言 哈希表主要是适合于快速查找某个元素(O(1)) 当我们要频繁的查找某个元素,第一哈希表O(1),第二,二分O(log n) 一般可以分为语言自带的容器哈希和用数组模拟的简易哈希。 最简单的比如数组模拟字符存储,只要开26个c

leetcode-24Swap Nodes in Pairs

带头结点。 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/public class Solution {public ListNode swapPairs(L

leetcode-23Merge k Sorted Lists

带头结点。 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/public class Solution {public ListNode mergeKLists

C++ | Leetcode C++题解之第393题UTF-8编码验证

题目: 题解: class Solution {public:static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num &

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟)

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟) 题目描述 给定一个链表,链表中的每个节点代表一个整数。链表中的整数由 0 分隔开,表示不同的区间。链表的开始和结束节点的值都为 0。任务是将每两个相邻的 0 之间的所有节点合并成一个节点,新节点的值为原区间内所有节点值的和。合并后,需要移除所有的 0,并返回修改后的链表头节点。 思路分析 初始化:创建一个虚拟头节点

C语言 | Leetcode C语言题解之第393题UTF-8编码验证

题目: 题解: static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num & MASK1) == 0) {return