当前位置:网站首页>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 .
边栏推荐
- Access is denied encountered when vfpbs calls excel under IIS
- [Motrix] download Baidu cloud files using Motrix
- Idea of capturing mobile terminal variant combination
- Qt之QListView的简单使用(含源码+注释)
- Xi'an Jiaotong automation control theory test simulation question [standard answer]
- AI大模型落地大考,浪潮交出了怎样的答卷?
- Basic operations of C language
- Gradient clip in dqn
- What are membrane stress and membrane strain
- Xi'an Jiaotong 21st autumn economics online homework answer sheet (III) [standard answer]
猜你喜欢

Xctf--Web--Challenge--area Wp

86. separate linked list

Why can transformer break into the CV world and kill CNN?

剑指 Offer 18. 删除链表的节点

The fourth day of learning C language for Asian people

Shenzhou ares tx6 boot logo modification tutorial

【LeetCode】236. Nearest common ancestor of binary tree

At the beginning of 2022, people who are ready to change jobs should pay attention to
![09- [istio] istio service entry](/img/48/86f8ec916201eefc6ca09c45a60a6a.jpg)
09- [istio] istio service entry

动态规划--怪盗基德的滑翔翼
随机推荐
Is it safe to open an account and trade with a compass?
Database SQL language 04 subquery and grouping function
Codeforces B. MEX and Array
Xctf--Web--Challenge--area Wp
Today, Ali came out with 35K. It's really sandpaper that wiped my ass. it showed me my hand
English语法_形容词/副词3级-最高级
Sword finger offer 29 Print matrix clockwise
Word frequency statistics (string, list)
The minecraft server address cannot be refreshed.
Attempt to redefine 'timeout' at line 2 solution
【板栗糖GIS】global mapper—如何把栅格的高程值赋予给点
E: Topic focus
Using lazy < t > in C # to realize singleton mode in WPF
Xijiao 21 autumn "motor and drive" online homework answer sheet (I) [standard answer]
We strongly recommend more than a dozen necessary plug-ins for idea development
Xi'an Jiaotong automation control theory test simulation question [standard answer]
Set of XXL job principles
Sound network, standing in the "soil" of the Internet of things
How to prevent source code leakage in enterprises and institutions
[Blue Bridge Road -- bug free code] DS1302 time module code analysis