lc 合并区间
链接:https://leetcode-cn.com/problems/merge-intervals/
代码:
class Solution { public: // [[2,3],[4,5],[6,7],[8,9],[1,10]] static bool myfunc(vector<int> i1, vector<int> i2) { return i1[0] < i2[0]; } vector<vector<int>> merge(vector<vector<int>>& intervals) { int n = intervals.size(); vector<vector<int>> res = {}; if(n == 0) return {}; sort(intervals.begin(), intervals.end(), myfunc); for(int i = 0; i < n; i++) { int last = res.size()-1; if(res.size() == 0 || res[last][1]< intervals[i][0]) { res.push_back(intervals[i]); } else { res[last][1] = max(res[last][1], intervals[i][1]); } } return res; } };
思路:想要不重复的话就必须要排序,如何做到不重不漏?
posted on 2020-05-23 00:06 FriskyPuppy 阅读(181) 评论(0) 编辑 收藏 举报