BM89. 合并区间
描述
示例1
输入:
[[10,30],[20,60],[80,100],[150,180]]
输出:
[[10,60],[80,100],[150,180]]
示例2
输入:
[[0,10],[10,20]]
输出:
[[0,20]]
C++ 解法, 执行用时: 43ms, 内存消耗: 14264KB, 提交时间: 2022-07-13
/** * Definition for an interval. * struct Interval { * int start; * int end; * Interval() : start(0), end(0) {} * Interval(int s, int e) : start(s), end(e) {} * }; */ static const auto io_sync_off = []() { std::ios::sync_with_stdio(false);//关闭输入输出 std::cin.tie(nullptr);//取消两个stream绑定 std::cout.tie(nullptr);//取消cin 和 cout之间的绑定,加快执行效率 return nullptr; }(); class Solution { public: vector<Interval> merge(vector<Interval>& intervals) { if (intervals.size() < 2) return intervals; sort(intervals.begin(), intervals.end(), [](Interval a, Interval b) {return a.start < b.start; }); Interval inter = intervals[0]; vector<Interval> ans; for (int i = 1; i < intervals.size(); i++) { if (intervals[i].start <= inter.end) { inter.end = inter.end < intervals[i].end ? intervals[i].end : inter.end; } else { ans.push_back(inter); inter = intervals[i]; } } ans.push_back(inter); return ans; } };
C++ 解法, 执行用时: 43ms, 内存消耗: 16208KB, 提交时间: 2022-06-21
/** * Definition for an interval. * struct Interval { * int start; * int end; * Interval() : start(0), end(0) {} * Interval(int s, int e) : start(s), end(e) {} * }; */ static const auto io_sync_off = []() { std::ios::sync_with_stdio(false);//关闭输入输出 std::cin.tie(nullptr);//取消两个stream绑定 std::cout.tie(nullptr);//取消cin 和 cout之间的绑定,加快执行效率 return nullptr; }(); class Solution { public: vector<Interval> merge(vector<Interval>& intervals) { if (intervals.size() < 2) return intervals; sort(intervals.begin(), intervals.end(), [](Interval a, Interval b) {return a.start < b.start; }); Interval inter = intervals[0]; vector<Interval> ans; for (int i = 1; i < intervals.size(); i++) { if (intervals[i].start <= inter.end) { inter.end = inter.end < intervals[i].end ? intervals[i].end : inter.end; } else { ans.push_back(inter); inter = intervals[i]; } } ans.push_back(inter); return ans; } };
C++ 解法, 执行用时: 44ms, 内存消耗: 13524KB, 提交时间: 2022-06-28
static const auto io_sync_off = []() { std::ios::sync_with_stdio(false);//关闭输入输出 std::cin.tie(nullptr);//取消两个stream绑定 std::cout.tie(nullptr);//取消cin 和 cout之间的绑定,加快执行效率 return nullptr; }(); class Solution { public: vector<Interval> merge(vector<Interval> &intervals) { vector<Interval> ret; int len = intervals.size(); sort(intervals.begin(), intervals.end(), [](Interval& a, Interval& b) {return a.start < b.start; }); intervals.push_back(Interval(INT_MAX, INT_MAX)); for (int i = 0; i < len; i++) { if (intervals[i].end >= intervals[i + 1].end) { intervals[i + 1]=intervals[i]; } else if(intervals[i].end >= intervals[i + 1].start) { intervals[i + 1].start = intervals[i].start; } else { ret.push_back(intervals[i]); } } return ret; } };
C++ 解法, 执行用时: 46ms, 内存消耗: 13388KB, 提交时间: 2022-07-31
/** * Definition for an interval. * struct Interval { * int start; * int end; * Interval() : start(0), end(0) {} * Interval(int s, int e) : start(s), end(e) {} * }; */ static const auto io_sync_off = []() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); return nullptr; }(); class Solution { public: vector<Interval> merge(vector<Interval> &intervals) { if (intervals.size() < 2) return intervals; sort(intervals.begin(), intervals.end(), [](Interval a, Interval b) { return a.start < b.start; }); vector<Interval> results; results.push_back(intervals[0]); for (int i = 1; i < intervals.size(); i++) { if (intervals[i].start <= results.back().end) { results.back().end = results.back().end >= intervals[i].end ? results.back().end : intervals[i].end; } else { results.push_back(intervals[i]); } } return results; } };
C++ 解法, 执行用时: 46ms, 内存消耗: 14032KB, 提交时间: 2022-07-05
/** * Definition for an interval. * struct Interval { * int start; * int end; * Interval() : start(0), end(0) {} * Interval(int s, int e) : start(s), end(e) {} * }; */ static const auto io_sync_off = []() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); return nullptr; }(); class Solution { public: vector<Interval> merge(vector<Interval>& intervals) { if (intervals.size() < 2) return intervals; sort(intervals.begin(), intervals.end(), [](Interval a, Interval b) {return a.start < b.start; }); Interval inter = intervals[0]; vector<Interval> ans; for (int i = 1; i < intervals.size(); i++) { if (intervals[i].start <= inter.end) { inter.end = inter.end < intervals[i].end ? intervals[i].end : inter.end; } else { ans.push_back(inter); inter = intervals[i]; } } ans.push_back(inter); return ans; } };