Skip to content

Commit 26dbaf6

Browse files
authored
Create 986.Interval-List-Intersections.cpp
1 parent e3a4ae9 commit 26dbaf6

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> intervalIntersection(vector<vector<int>>& firstList, vector<vector<int>>& secondList)
4+
{
5+
int m = firstList.size();
6+
int n = secondList.size();
7+
int i = 0, j = 0;
8+
vector<vector<int>>rets;
9+
while (i<m && j<n)
10+
{
11+
if (firstList[i][1]<secondList[j][0])
12+
i++;
13+
else if (firstList[i][0]>secondList[j][1])
14+
j++;
15+
else
16+
{
17+
int start = max(firstList[i][0], secondList[j][0]);
18+
int end = min(firstList[i][1], secondList[j][1]);
19+
rets.push_back({start, end});
20+
if (firstList[i][1]<secondList[j][1])
21+
i++;
22+
else
23+
j++;
24+
}
25+
}
26+
return rets;
27+
28+
}
29+
};

0 commit comments

Comments
 (0)