iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >C++实现LeetCode(57.插入区间)
  • 512
分享到

C++实现LeetCode(57.插入区间)

2024-04-02 19:04:59 512人浏览 薄情痞子
摘要

[LeetCode] 57. Insert Interval 插入区间 Given a set of non-overlapping intervals, ins

[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:

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].

NOTE: input types have been changed on April 15, 2019. Please reset to default code definition to get new method signature.

这道题让我们在一系列非重叠的区间中插入一个新的区间,可能还需要和原有的区间合并,可以对给定的区间集进行一个一个的遍历比较,那么会有两种情况,重叠或是不重叠,不重叠的情况最好,直接将新区间插入到对应的位置即可,重叠的情况比较复杂,有时候会有多个重叠,需要更新新区间的范围以便包含所有重叠,之后将新区间加入结果 res,最后将后面的区间再加入结果 res 即可。具体思路是,用一个变量 cur 来遍历区间,如果当前 cur 区间的结束位置小于要插入的区间的起始位置的话,说明没有重叠,则将 cur 区间加入结果 res 中,然后 cur 自增1。直到有 cur 越界或有重叠 while 循环退出,然后再用一个 while 循环处理所有重叠的区间,每次用取两个区间起始位置的较小值,和结束位置的较大值来更新要插入的区间,然后 cur 自增1。直到 cur 越界或者没有重叠时 while 循环退出。之后将更新好的新区间加入结果 res,然后将 cur 之后的区间再加入结果 res 中即可,参见代码如下:

解法一:


class Solution {
public:
    vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval) {
        vector<vector<int>> res;
        int n = intervals.size(), cur = 0;
        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;
    }
};

下面这种方法的思路跟上面的解法很像,只不过没有用 while 循环,而是使用的是 for 循环,但是思路上没有太大的区别,变量 cur 还是用来记录新区间该插入的位置,稍有不同的地方在于在 for 循环中已经将新区间后面不重叠的区间也加进去了,for 循环结束后就只需要插入新区间即可,参见代码如下:

解法二:


class Solution {
public:
    vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval) {
        vector<vector<int>> res;
        int n = intervals.size(), cur = 0;
        for (int i = 0; i < n; ++i) {
            if (intervals[i][1] < newInterval[0]) {
                res.push_back(intervals[i]);
                ++cur;
            } else if (intervals[i][0] > newInterval[1]) {
                res.push_back(intervals[i]);
            } else {
                newInterval[0] = min(newInterval[0], intervals[i][0]);
                newInterval[1] = max(newInterval[1], intervals[i][1]);
            }
        }
        res.insert(res.begin() + cur, newInterval);
        return res;
    }
};

下面这种解法就是把上面解法的 for 循环改为了 while 循环,其他的都没有变,代码如下:

解法三:


class Solution {
public:
    vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval) {
        vector<vector<int>> res;
        int n = intervals.size(), cur = 0, i = 0;
        while (i < n) {
            if (intervals[i][1] < newInterval[0]) {
                res.push_back(intervals[i]);
                ++cur;
            } else if (intervals[i][0] > newInterval[1]) {
                res.push_back(intervals[i]);
            } else {
                newInterval[0] = min(newInterval[0], intervals[i][0]);
                newInterval[1] = max(newInterval[1], intervals[i][1]);
            }
            ++i;
        }
        res.insert(res.begin() + cur, newInterval);
        return res;
    }
};

到此这篇关于c++实现LeetCode(57.插入区间)的文章就介绍到这了,更多相关C++实现插入区间内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: C++实现LeetCode(57.插入区间)

本文链接: https://www.lsjlt.com/news/130599.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • C++实现LeetCode(57.插入区间)
    [LeetCode] 57. Insert Interval 插入区间 Given a set of non-overlapping intervals, ins...
    99+
    2024-04-02
  • 【每日一题】57. 插入区间
    【每日一题】57. 插入区间 57. 插入区间题目描述解题思路 57. 插入区间 题目描述 给你一个 无重叠的 ,按照区间起始端点排序的区间列表。 在列表中插入一个新的区间,你需要确保列表中的区间仍然有序且不重叠(如果有必要...
    99+
    2023-08-30
    leetcode 算法 职场和发展
  • C++实现LeetCode(163.缺失区间)
    [LeetCode] 163. Missing Ranges 缺失区间 Given a sorted integer array nums, where the ...
    99+
    2024-04-02
  • C++实现LeetCode(56.合并区间)
    [LeetCode] 56. Merge Intervals 合并区间 Given a collection of intervals, merge all overlapping ...
    99+
    2024-04-02
  • C++实现LeetCode(228.总结区间)
    [LeetCode] 228.Summary Ranges 总结区间 Given a sorted integer array without duplicates, return ...
    99+
    2024-04-02
  • C++怎么插入区间
    本篇内容介绍了“C++怎么插入区间”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!插入区间Given a set of non-ov...
    99+
    2023-06-20
  • C++实现LeetCode(147.链表插入排序)
    [LeetCode] 147. Insertion Sort List 链表插入排序 Sort a linked list using insertion sort. A graph...
    99+
    2024-04-02
  • C++实现LeetCode(35.搜索插入位置)
    [LeetCode] 35. Search Insert Position 搜索插入位置 Given a sorted array and a target value, retur...
    99+
    2024-04-02
  • C++实现LeetCode(88.混合插入有序数组)
    [LeetCode] 88. Merge Sorted Array 混合插入有序数组 Given two sorted integer arrays nums1 ...
    99+
    2024-04-02
  • C++实现LeetCode(21.混合插入有序链表)
    [LeetCode] 21. Merge Two Sorted Lists 混合插入有序链表 Merge two sorted linked lists and return it ...
    99+
    2024-04-02
  • C++实现LeetCode之区间的示例分析
    这篇文章将为大家详细讲解有关C++实现LeetCode之区间的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。[LeetCode] 228.Summary Ranges 总结区间Given a so...
    99+
    2023-06-20
  • C++实现LeetCode之缺失区间的示例分析
    这篇文章给大家分享的是有关C++实现LeetCode之缺失区间的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。[LeetCode] 163. Missing Ranges 缺失区间Given a sort...
    99+
    2023-06-20
  • C++实现LeetCode(130.包围区域)
    [LeetCode] 130. Surrounded Regions 包围区域 Given a 2D board containing 'X' and ...
    99+
    2024-04-02
  • C++实现LeetCode(164.求最大间距)
    [LeetCode] 164. Maximum Gap 求最大间距 Given an unsorted array, find the maximum difference betw...
    99+
    2024-04-02
  • C++实现LeetCode(904.水果装入果篮)
    [LeetCode] 904. Fruit Into Baskets 水果装入果篮 In a row of trees, the `i`-th tree prod...
    99+
    2024-04-02
  • goland中使用leetcode插件实现
    goland leetcode 插件安装可以提高刷题效率,对于学习算法的同学是个不错的选择 安装使用步骤: 安装插件: a. 左上角Goland -> Preferencesb...
    99+
    2023-05-16
    goland leetcode插件 goland leetcode
  • C#/VB.NET实现在Word中插入水印
    目录前言安装在 Word 文档中插入文本水印在 Word 文档中插入图片水印前言 水印是指在 Word 文档的背景中以淡色或灰色显示的文本或图像。它们可用于声明文档的机密性、版权或其...
    99+
    2022-11-13
    C# Word 插入水印  VB.NET实现 Word 插入水印 
  • C++怎么实现链表插入排序
    本篇内容主要讲解“C++怎么实现链表插入排序”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“C++怎么实现链表插入排序”吧!链表插入排序链表的插入排序实现原理很简单,就是一个元素一个元素的从原链表...
    99+
    2023-06-20
  • C++如何实现LeetCode
    这篇文章给大家分享的是有关C++如何实现LeetCode的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。[LeetCode] Reverse Linked List 倒置链表Reverse a singly lin...
    99+
    2023-06-20
  • C++怎么实现LeetCode
    这篇文章主要介绍“C++怎么实现LeetCode”,在日常操作中,相信很多人在C++怎么实现LeetCode问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C++怎么实现LeetCode”的疑惑有所帮助!接下来...
    99+
    2023-06-20
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作