【细节实现题】LeetCode 56. Merge Intervals

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Allenlzcoder/article/details/81592969

LeetCode 56. Merge Intervals

Solution1:我的答案

这道题思路不能,有个坑爹的地方在于输入的区间向量未必是有序的,所以利用优先队列priority_queue来排序

/**
 * Definition for an interval.
 * struct Interval {
 *     int start;
 *     int end;
 *     Interval() : start(0), end(0) {}
 *     Interval(int s, int e) : start(s), end(e) {}
 * };
 */
struct cmp {//学习定义priority_queue的比较函数
    bool operator () (Interval &a, Interval &b) {
        return a.start > b.start;
    }
};
class Solution { // O(n)
public:
    vector<Interval> merge(vector<Interval>& intervals) {
        if (!intervals.size()) return {};
        priority_queue<Interval, vector<Interval>, cmp> q(intervals.begin(), intervals.end());
        vector<Interval> new_intervals;
        while (!q.empty()) { //注意优先队列不能用迭代器
            new_intervals.push_back(q.top());
            q.pop();
        }   
        vector<Interval> res;
        res.push_back(new_intervals[0]);
        for (int i = 1;  i < new_intervals.size(); i++) {
            if (res.back().end < new_intervals[i].start) {
                res.push_back(new_intervals[i]);                  
            } else {
                res.back().start = min(res.back().start, new_intervals[i].start);
                res.back().end = max(res.back().end, new_intervals[i].end);
            }
        }
        return res;
    }
};

priority_queue用法

官网链接:http://www.cplusplus.com/reference/queue/priority_queue/?kw=priority_queue
自定义比较函数连接:https://blog.csdn.net/Allenlzcoder/article/details/80885979
注意:priority_queue没有迭代器
常用的函数有
empty(): Test whether container is empty (public member function )
size(): Return size (public member function )
top(): Access top element (public member function )
push(): Insert element (public member function )
emplace(): Construct and insert element (public member function )
pop(): Remove top element (public member function )
swap(): Swap contents (public member function )

Solution2:参考答案

链接:http://www.cnblogs.com/grandyang/p/4370601.html

/**
 * Definition for an interval.
 * struct Interval {
 *     int start;
 *     int end;
 *     Interval() : start(0), end(0) {}
 *     Interval(int s, int e) : start(s), end(e) {}
 * };
 */
bool cmp (Interval &a, Interval &b) {
    return a.start < b.start;
}
class Solution {
public:
    vector<Interval> merge(vector<Interval>& intervals) {
        if (intervals.empty()) return {};
        //关于sort()函数自定义比较函数还是得学习一个!
        sort(intervals.begin(), intervals.end(), cmp);
        vector<Interval> res{intervals[0]};
        for (int i = 1; i < intervals.size(); ++i) {
            if (res.back().end < intervals[i].start) {
                res.push_back(intervals[i]);
            } else {
                res.back().end = max(res.back().end, intervals[i].end);
            }
        }   
        return res;
    }
};

关于sort()的自定义比较函数,参考网址:https://blog.csdn.net/pmt123456/article/details/61191348
写在类内时,要加上static,lexicographical_compare 最后要求的是一个普通函数指针,而不是成员函数指针,所以要加static。
都忘了,是时候好好复习一下了~~~

猜你喜欢

转载自blog.csdn.net/Allenlzcoder/article/details/81592969