2023-08-28力扣每日一题-忘记发了

链接:

57. 插入区间

题意:

如题

解:

怎么会有人写完题忘记做笔记啊,恼

懒了,排序

实际代码:

#include
using namespace std;
static bool cmp(const vector& A,const vector& B)
{
	if(A[0]==B[0]) return A[1]>B[1];
	else return A[0]> insert(vector>& intervals, vector& newInterval)
{
    intervals.push_back(newInterval);
    sort(intervals.begin(),intervals.end(),cmp);
    vector>ans;
    
    int l=-1,r=-1;
    for(auto row:intervals)
    {
    	if(l==-1)
    	{
    		l=row[0];r=row[1];
		}
		else
		{
			if(row[0]<=r)
			{
				r=max(row[1],r);
			}
			else
			{
				ans.push_back(vector{l,r});
				l=row[0];r=row[1];
			}
		}
	}
	ans.push_back(vector{l,r});
	return ans;
}
int main()
{
	int n;cin>>n;
	vector> intervals;
	vectorinterval;
	for(int i=0;i>a>>b;
		interval={a,b};
		intervals.push_back(interval);
	}
	int a,b;cin>>a>>b;
	interval={a,b};
	vector>ans=insert(intervals,interval);
	for(auto row:ans)
	{
		for(auto col:row) cout<

限制:

  • 0 <= intervals.length <= 104
  • intervals[i].length == 2
  • 0 <= intervals[i][0] <= intervals[i][1] <= 105
  • intervals 根据 intervals[i][0] 按 升序 排列
  • newInterval.length == 2
  • 0 <= newInterval[0] <= newInterval[1] <= 105

你可能感兴趣的:(力扣每日一题,leetcode)