LeetCode 56. Merge Intervals

题目

简单的结构体排序

struct Node
{
    int x;
    int y;
    Node(){}
    Node(int x,int y){
        this->x = x;
        this->y =y;
    }
}a[100005];

int Compare(Node a,Node b)
{
    if(a.x==b.x)
    {
       return a.y<b.y;
    }
    return a.x<b.x;
}
class Solution {
public:
    vector<vector<int>> merge(vector<vector<int>>& intervals) {
        
        vector<vector<int>> ans;
        vector<int> res;
        if(intervals.size()==0)
            return ans;
        int pos=0;
        for(int i=0;i<intervals.size();i++)
        {
            a[pos++] = Node(intervals[i][0],intervals[i][1]);
        }
        
        sort(a,a+pos,Compare);
        
      
        int start =a[0].x;
        int end=a[0].y;
        for(int i=1;i<pos;i++)
        {
            if(a[i].x<=end)
            {
                end=max(end,a[i].y);
            }
            else
            {
                res.clear();
                res.push_back(start);
                res.push_back(end);
                ans.push_back(res);
                
                start=a[i].x;
                end=a[i].y;
            }
        }
        
        res.clear();
        res.push_back(start);
        res.push_back(end);
        ans.push_back(res);
        
        return ans;
        
    }
};


上一篇:当值改变时,MySQL增加用户变量


下一篇:python – 间隔的天数总和