当前位置:网站首页>Leetcode56. consolidation interval
Leetcode56. consolidation interval
2022-06-30 05:47:00 【Day by day, day by day, day by day!!】
One : The analects of Confucius
It feels hard to do this , Because there are few falls , So many things are simply exquisite , Speed , As one can imagine , The results were all bad .
Two : subject

3、 ... and : Upper code
// class Solution {
// public:
// static bool cmp(const vector<int>&v1, const vector<int>& v2) {
// return v1[1] < v2[1];
// }
// vector<vector<int>> merge(vector<vector<int>>& intervals) {
// /**
// Ideas :1. This question is similar to the non overlapping interval
// 2. Let's start with the left bound of the interval array in ascending order
// 3. Then judge the right boundary of the first group of elements and compare it with the left boundary of the second group of elements , If it's larger, merge , Simultaneous updating
// Right border .
// */
// vector<vector<int> >ans;
// int flag = 0;
// if(intervals.size() == 1) {
// ans.push_back(intervals[0]);
// }
// sort(intervals.begin(),intervals.end(),cmp);
// int end = intervals[0][1];// The right boundary of the first set of elements
// int start = intervals[0][0];// The left boundary of the first set of elements
// if(intervals.size() > 1 && intervals[1][0] > intervals[0][1]) {// Special treatment of the first group of elements
// ans.push_back(intervals[0]);
// }
// for(int i = 1; i < intervals.size(); i++) {
// while(i < intervals.size() && end >= intervals[i][0]) {// Merge if it is larger than its left boundary
// end = intervals[i][1];
// start = min(start,intervals[i][0]);
// i++;
// flag = 1;
// if(i == intervals.size()) ans.push_back({start,end});
// }
// if(i < intervals.size() && flag == 1){
// ans.push_back({start,end});
// flag = 0;
// }
// if(i < intervals.size() && end < intervals[i][0]) {
// ans.push_back(intervals[i]);
// }
// }
// return ans;
// }
// };
class Solution {
public:
static bool cmp(const vector<int>&v1, const vector<int>& v2) {
return v1[0] < v2[0];
}
vector<vector<int>> merge(vector<vector<int>>& intervals) {
/** Ideas :1. This question is similar to the non overlapping interval 2. Let's start with the left bound of the interval array in ascending order 3. Then judge the left boundary of the second group of elements and compare it with the right boundary of the first group of elements , If it's larger, merge , Simultaneous updating Right border . 4.intervals[i-1][1] >= inteavals[i][0]; Then we can judge the overlap , Because we do it according to the left bound of the array ascend , So intervals[i][0] > intervals[i-1][0], Then there must be overlap . */
vector<vector<int> >ans;
int flag = 0;
sort(intervals.begin(),intervals.end(),cmp);
for(int i = 1; i < intervals.size(); i++) {
int start = intervals[i-1][0];// there i-1, Dug a hole for our last set of elements , If the last set of elements is not merged
int end = intervals[i-1][1]; // there i-1 Will separate them .
while(i < intervals.size() && intervals[i][0] <= end) {
// The right boundary of the previous element is greater than the left boundary of the current element
end = max(end,intervals[i][1]);// Because maybe the right boundary is not in ascending order
if(i == intervals.size()-1) flag = 1;
i++;
}
ans.push_back({
start,end});
}
if(flag == 0) ans.push_back(intervals[intervals.size()-1]);
return ans;
}
};

For two hours , I can't write it myself , Dystocia , You can't give birth even if you're tired to death , I used by the right boundary of the array Ascending processing , I thought it was similar to the problem I had done before , But it's not , We deal with the right boundary if people are in ascending order , So when there is a set of ranges covering all array ranges
We'll have trouble giving birth , But you won't encounter this problem with the left boundary .
What kind of feelings are firm ,
From university to society ,
To get married again ?
Do ordinary things ,
We are ordinary people
But I always feel like a Superhero
Ha ha ha Nonsense over Good night, Come on, stranger .
边栏推荐
- [Motrix] download Baidu cloud files using Motrix
- STM32F103系列控制的OLED IIC 4针
- 3D rotation album
- 雲服務器部署 Web 項目
- Xi'an Jiaotong automation control theory test simulation question [standard answer]
- OpenCL线程代数库ViennaCL的使用
- Promise知识点拾遗
- The minecraft server address cannot be refreshed.
- At the beginning of 2022, people who are ready to change jobs should pay attention to
- 动态规划--怪盗基德的滑翔翼
猜你喜欢

Today, Ali came out with 35K. It's really sandpaper that wiped my ass. it showed me my hand

剑指 Offer 22. 链表中倒数第k个节点

14x1.5cm vertical label is a little difficult, VFP calls bartender to print

Shenzhou ares tx6 boot logo modification tutorial

企事业单位源代码防泄露工作该如何进行

Switch to software testing and report to the training class for 3 months. It's a high paying job. Is it reliable?

How does WPS cancel automatic numbering? Four options

9. naive Bayes

Redistemplate common method summary

UE4_ Editor development: highlight the UI making method according to the assets dragged by the mouse (1)
随机推荐
The definition of strain was originally from stretch_ Ratio started
86. separate linked list
English grammar_ Adjective / adverb Level 3 - superlative
剑指 Offer 18. 删除链表的节点
Database SQL language 04 subquery and grouping function
Rotating frame target detection mmrotate v0.3.1 training dota data set (II)
Implementation of property management system with ssm+ wechat applet
How to judge the quality of network transformer? What symptom is network filter transformer broken?
Rotating box target detection mmrotate v0.3.1 getting started
2022年,谁在推动音视频产业的新拐点?
The average salary of software testing in 2022 has been released. Have you been averaged?
How does WPS cancel automatic numbering? Four options
Access is denied encountered when vfpbs calls excel under IIS
hashlips_ art_ Engine-1.0.6 usage
炒股用指南针开户交易安全吗?
Xijiao 21 autumn "motor and drive" online homework answer sheet (I) [standard answer]
10-【istio】istio Sidecar
Detailed explanation of issues related to SSL certificate renewal
Here comes the nearest chance to Ali
/Path/to/ idiom, not a command