NC390. 区间列表交集
描述
示例1
输入:
[[0,2],[5,10],[11,13]],[[1,3],[5,11]]
输出:
[[1,2],[5,10],[11,11]]
C++ 解法, 执行用时: 5ms, 内存消耗: 660KB, 提交时间: 2022-07-24
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param first int整型vector<vector<>> * @param second int整型vector<vector<>> * @return int整型vector<vector<>> */ vector<vector<int> > intersectionofintervals(vector<vector<int> >& first, vector<vector<int> >& second) { vector<vector<int> > ans; int p1 = 0, p2 = 0; while (p1 < first.size() && p2 < second.size()) { if (first[p1][1] < second[p2][0]) { ++p1; continue; } if (second[p2][1] < first[p1][0]) { ++p2; continue; } ans.push_back({max(first[p1][0], second[p2][0]), min(first[p1][1], second[p2][1])}); if (first[p1][1] < second[p2][1]) { ++p1; } else { ++p2; } } return ans; } };
C++ 解法, 执行用时: 5ms, 内存消耗: 660KB, 提交时间: 2022-05-19
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param first int整型vector<vector<>> * @param second int整型vector<vector<>> * @return int整型vector<vector<>> */ vector<vector<int> > intersectionofintervals(vector<vector<int> >& first, vector<vector<int> >& second) { // write code here vector<vector<int> > ans; int p1 = 0, p2 = 0; while (p1 < first.size() && p2 < second.size()) { if (first[p1][1] < second[p2][0]) { ++p1; continue; } if (second[p2][1] < first[p1][0]) { ++p2; continue; } ans.push_back({max(first[p1][0], second[p2][0]), min(first[p1][1], second[p2][1])}); if (first[p1][1] < second[p2][1]) { ++p1; } else { ++p2; } } return ans; } };
C++ 解法, 执行用时: 5ms, 内存消耗: 680KB, 提交时间: 2022-06-13
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param first int整型vector<vector<>> * @param second int整型vector<vector<>> * @return int整型vector<vector<>> */ vector<vector<int> > intersectionofintervals(vector<vector<int> >& first, vector<vector<int> >& second) { // write code here vector<vector<int>> result; int left=0,right=0; int n=first.size(); int m=second.size(); while(left<n&&right<m){ auto f1=first[left]; auto f2=second[right]; if(f1[0]<=f2[0]){ if(f2[1]<=f1[1]){ result.push_back({f2[0],f2[1]}); right++; }else{ if(f1[1]>=f2[0]){ result.push_back({f2[0],f1[1]}); left++; }else{ left++; } } } else{ if(f1[1]<=f2[1]){ result.push_back({f1[0],f1[1]}); left++; }else{ if(f2[1]>=f1[0]){ result.push_back({f1[0],f2[1]}); right++; }else{ right++; } } } } return result; } };
C++ 解法, 执行用时: 5ms, 内存消耗: 684KB, 提交时间: 2022-04-11
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param first int整型vector<vector<>> * @param second int整型vector<vector<>> * @return int整型vector<vector<>> */ static bool cmp(vector<int>& a, vector<int>& b) { if (a[0] == b[0]) return a[1] < b[1]; return a[0] < b[0]; } vector<vector<int> > intersectionofintervals(vector<vector<int> >& first, vector<vector<int> >& second) { // write code here vector<vector<int>> res; // 1. 排序, 但是本题已经排过序,所以不用再排序 int n1 = first.size(), n2 = second.size(); int i = 0, j = 0; while (i < n1 && j < n2) { int a1 = first[i][0], a2 = first[i][1]; int b1 = second[j][0], b2 = second[j][1]; // a2 < b1 or b2 < a1 时没有交集 if (a2 >= b1 && b2 >= a1) { res.push_back({max(a1, b1), min(a2, b2)}); } if (a2 < b2) { i++; } else { j++; } } return res; } };
C++ 解法, 执行用时: 6ms, 内存消耗: 652KB, 提交时间: 2022-08-06
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param first int整型vector<vector<>> * @param second int整型vector<vector<>> * @return int整型vector<vector<>> */ vector<vector<int> > intersectionofintervals(vector<vector<int> >& first, vector<vector<int> >& second) { // write code here vector<vector<int>> ans; int p1=0,p2=0; while(p1<first.size()&&p2<second.size()) { if(first[p1][1]<second[p2][0]) { ++p1; continue; } if(second[p2][1]<first[p1][0]) { ++p2; continue; } ans.push_back({max(first[p1][0],second[p2][0]),min(first[p1][1],second[p2][1])}); if(first[p1][1]<second[p2][1]) { ++p1; }else{ ++p2; } } return ans; } };