当前位置:网站首页>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 recordtmp
There 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 recordtmp
There is no intersection , thattmp
Is an interval in the answer , Record in answer , And thentmp
Update to the current intervalintervals[i]
, Continue traversing . - After the traversal is over , Put the interval
tmp
Record 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;
}
};
边栏推荐
- Introduction to opencv (I): image reading and display
- Introduction notes to pytorch deep learning (XII) neural network - nonlinear activation
- Wechat applet reports errors using vant web app
- Final review -php learning notes 1
- 架构实战营模块 5 作业
- Deep learning - brnn and DRNN
- 期末复习-PHP学习笔记1
- JS代码案例
- 【花雕体验】13 搭建ESP32C3之PlatformIO IDE开发环境
- December 4, 2021 - Introduction to macro genome analysis process tools
猜你喜欢
Combinatorial mathematics Chapter 1 Notes
November 9, 2020 [wgs/gwas] - whole genome analysis (association analysis) process (Part 2)
Recurrence relation (difference equation) -- Hanoi problem
深度学习——语言模型和序列生成
What management improvements can CRM bring to enterprises
CRM能为企业带来哪些管理提升
多快好省,低门槛AI部署工具FastDeploy测试版来了!
Full stack performance testing theory - Summary
C # about Net cognition
February 14, 2022 [reading notes] - life science based on deep learning Chapter 2 Introduction to deep learning (Part 1)
随机推荐
Opencv4.2.0+vs2015 configuration
Wechat applet reports errors using vant web app
Cadence physical library lef file syntax learning [continuous update]
Go 数据类型篇之字符串及底层字符类型
【笔记】Polygon mesh processing 学习笔记(10)
Palindrome substring, palindrome subsequence
Hit the industry directly | the flying propeller launched the industry's first model selection tool
1163 Dijkstra Sequence
The counting tool of combinatorial mathematics -- generating function
[JUC series] overview of fork/join framework
1162 Postfix Expression
架构实战营模块 5 作业
Miracle Mu server rental selection is real and easy to use, stable and intrusion proof
Deep learning - bounding box prediction
Want to ask, how to choose securities companies for stock speculation? Is it safe to open an account online?
Construction of module 5 of actual combat Battalion
C # about Net cognition
【NVMe2.0b 14-6】Format NVM、Keep Alive、Lockdown command
Deep learning -- using word embedding and word embedding features
Wsl2 using GPU for deep learning