当前位置:网站首页>Sword finger offer II 074 Merge interval (sort, array)
Sword finger offer II 074 Merge interval (sort, array)
2022-06-30 08:05:00 【Lafiteeee】
Title Description
A simple merging interval problem . Subject portal 
Their thinking
If the two intervals can be merged , There must be an intersection , That is, the head of the second interval is in the first interval , In this way, I thought of sorting all the intervals first , Then use an array tmp[] Record the starting point of the current interval . At the beginning, the starting point is set as the first interval intervals[0] The starting point of , Then traverse all the intervals :
- If the current interval
intervals[i]The starting point oftmp[0]andtmp[1]Between , That is, the merge interval with the current recordtmpThere is intersection , So merge the two intervals , holdtmp[1]Is changed tomax(tmp[1], intervals[i][1])( Here is the maximum of the two values , Easy to ignore ); - If the current interval
intervals[i]The starting point is greater thantmp[1], That is, the merge interval with the current recordtmpThere is no intersection , thattmpIs an interval in the answer , Record in answer , And thentmpUpdate to the current intervalintervals[i], Continue traversing . - After the traversal is over , Put the interval
tmpRecord it in the answer .
Code
class Solution {
public:
vector<vector<int>> merge(vector<vector<int>>& intervals) {
vector<int> tmp(2);
vector<vector<int>> ans;
sort(intervals.begin(), intervals.end(), [&](const vector<int>& a, const vector<int>& b) {
return a[0] < b[0];
}); // Sort
for (int i = 0; i < intervals.size(); i++) {
if (i == 0) {
tmp = intervals[i]; // initialization tmp Section
}
if (intervals[i][0] >= tmp[0] && intervals[i][0] <= tmp[1]) {
// There is intersection
tmp[1] = max(tmp[1], intervals[i][1]); // Merge range
} else {
// No intersection
ans.push_back(tmp); // Add answers
tmp = intervals[i]; // Reset tmp Section
}
}
ans.push_back(tmp);
return ans;
}
};
边栏推荐
- [notes] polygon mesh processing learning notes (10)
- 直击产业落地 | 飞桨重磅推出业界首个模型选型工具
- Examen final - notes d'apprentissage PHP 5 - Tableau PHP
- 【JUC系列】Fork/Join框架之概览
- Deep learning - embedding matrix and learning word embedding andword2vec
- 鲸探NFT数字臧品系统开发技术分享
- C# Console. Writeline() function output format
- Final review -php learning notes 5-php array
- 【花雕体验】12 搭建ESP32C3之Arduino开发环境
- Halcon12+vs2013 C # configuration
猜你喜欢
![Cadence physical library lef file syntax learning [continuous update]](/img/5a/b42269d80c13779762a8da67ba6989.jpg)
Cadence physical library lef file syntax learning [continuous update]
![2021.11.20 [reading notes] | differential variable splicing events and DTU analysis](/img/02/6971454e51c015990b5b60b357ee1d.jpg)
2021.11.20 [reading notes] | differential variable splicing events and DTU analysis

深度学习——残差网络ResNets

December 4, 2021 - Introduction to macro genome analysis process tools

你了解IP协议吗?

Final review -php learning notes 5-php array

Use of nested loops and output instances

Deep learning vocabulary representation

期末复习-PHP学习笔记2-PHP语言基础

Deep learning -- using word embedding and word embedding features
随机推荐
深度学习——卷积的滑动窗口实现
深度学习——Bounding Box预测
深度学习——序列模型and数学符号
MySQL quotation sentence is unlocked: algorithm=insert, lock=none
ACM. Hj48 delete the node with the specified value from the one-way linked list ●●
【Tensorflow-gpu】window11下深度学习环境搭建
Deep learning vocabulary representation
【NVMe2.0b 14-6】Format NVM、Keep Alive、Lockdown command
JS代码案例
More, faster, better and cheaper. Here comes the fastdeploy beta of the low threshold AI deployment tool!
多快好省,低门槛AI部署工具FastDeploy测试版来了!
1162 Postfix Expression
Want to ask, how to choose securities companies for stock speculation? Is it safe to open an account online?
回文子串、回文子序列
深度学习——残差网络ResNets
Experiment 4 QT
6月底了,可以开始做准备了,不然这么赚钱的行业就没你的份了
ACM. HJ48 从单向链表中删除指定值的节点 ●●
Experiment 6 examination
November 22, 2021 [reading notes] - bioinformatics and functional genomics (Chapter 5, section 4, hidden Markov model)