• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

c++ 搞代码 4年前 (2022-01-06) 17次浏览 已收录 0个评论
文章目录[隐藏]

这篇文章主要介绍了C++实现LeetCode(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:

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> insert(vector<vector>& intervals, vector& newInterval) { vector<vector> 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]); <strong style="color:transparent">来源gaodaima#com搞(代@码网</strong>++cur; res.push_back(newinterval); n) return res; };<pre></div><p>下面这种方法的思路跟上面的解法很像,只不过没有用 while 循环,而是使用的是 for 循环,但是思路上没有太大的区别,变量 cur 还是用来记录新区间该插入的位置,稍有不同的地方在于在 for 循环中已经将新区间后面不重叠的区间也加进去了,for 循环结束后就只需要插入新区间即可,参见代码如下:</p><p>解法二:</p><div class="gaodaimacode"><pre class="prettyprint linenums"> class Solution { public: vector<vector> insert(vector<vector>& intervals, vector& newInterval) { vector<vector> res; int n = intervals.size(), cur = 0; for (int i = 0; i <n; ++i) { if (intervals[i][1]  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> insert(vector<vector>& intervals, vector& newInterval) { vector<vector> res; int n = intervals.size(), cur = 0, i = 0; while (i <n) { if (intervals[i][1]  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.插入区间)的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:C++实现LeetCode(57.插入区间)

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址