当前位置:网站首页>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 .
边栏推荐
- Vfpbs uploads excel and saves MSSQL to the database
- On line assignment of financial cost management in the 22nd spring of Western Polytechnic University [Full Score answer]
- OSPF - authentication and load balancing summary (including configuration commands)
- MySQL advanced (Advanced SQL statement)
- Acwing winter vacation daily question 2022 punch in day 11
- [untitled] user defined function
- The definition of strain was originally from stretch_ Ratio started
- 超简单 STM32 RTC闹钟 时钟配置
- You don't know how to deduce the location where HashSet stores elements?
- Rotating frame target detection mmrotate v0.3.1 learning configuration
猜你喜欢
How to create a CSR (certificate signing request) file?
The average salary of software testing in 2022 has been released. Have you been averaged?
How to judge the quality of network transformer? What symptom is network filter transformer broken?
C语言基础小操作
Baiwen.com 7 days Internet of things smart home learning experience punch in the third day
How to prevent source code leakage in enterprises and institutions
MySQL advanced (Advanced SQL statement)
OSPF - authentication and load balancing summary (including configuration commands)
Sword finger offer 22 The penultimate node in the linked list
8 ways to earn passive income
随机推荐
Access is denied encountered when vfpbs calls excel under IIS
Xi'an Jiaotong 21st autumn "computerized accounting" online homework answer sheet (I) [standard answer]
How to write a thesis
Database SQL language 05 SQL exercise
企事业单位源代码防泄露工作该如何进行
hashlips_ art_ Engine-1.0.6 usage
Transfer the token on the matic-erc20 network to the matic polygon
Sword finger offer 22 The penultimate node in the linked list
Responsive layout
Did you know that WPS can turn on eye protection mode?
Here comes the nearest chance to Ali
Xi'an Jiaotong 21st autumn online expansion resources of online trade and marketing (III) [standard answer]
Wechat applet training 2
Uboot reads the DDR memory size by sending 'R' characters through the terminal
动态规划--怪盗基德的滑翔翼
Terminal convenient SSH connection
PyGame. Why can't I exit when I click X in the window? I can only exit when I return idle
Lantern Festival | maoqiu technology and everyone "guess riddles and have a good night"
token 过期后,如何自动续期?
24、 I / O device model (serial port / keyboard / disk / printer / bus / interrupt controller /dma and GPU)