c_cpp 56.cpp

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 56.cpp相关的知识,希望对你有一定的参考价值。

/**
 * Definition for an interval.
 * struct Interval {
 *     int start;
 *     int end;
 *     Interval() : start(0), end(0) {}
 *     Interval(int s, int e) : start(s), end(e) {}
 * };
 */
class Solution {
    static bool comp(Interval x, Interval y) {
        return x.start < y.start || (x.start == y.start && x.end < y.end);
    }
public:
    vector<Interval> merge(vector<Interval>& intervals) {
        vector<Interval> result;
        if (intervals.size() == 0) return result;

        sort(intervals.begin(), intervals.end(), Solution::comp);
        
        int n_st = intervals[0].start, n_end = intervals[0].end;
        for(int i = 1; i < intervals.size(); i++) {
            if (intervals[i].start <= n_end) {
                if (intervals[i].end > n_end)
                    n_end = intervals[i].end;
            } else {
                result.push_back(Interval(n_st, n_end));
                n_st = intervals[i].start, n_end = intervals[i].end;
            }
        }
        result.push_back(Interval(n_st, n_end));
        return result;
    }
};

以上是关于c_cpp 56.cpp的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp 200.岛屿数量

c_cpp 127.单词阶梯

c_cpp MOFSET

c_cpp MOFSET

c_cpp 31.下一个排列

c_cpp string→char *