Skip to content

Commit 1139b6e

Browse files
committed
feat: update solutions to lc problem: No.0986
No.0986.Interval List Intersections
1 parent 0a2e473 commit 1139b6e

File tree

11 files changed

+87
-87
lines changed

11 files changed

+87
-87
lines changed

lcof2/剑指 Offer II 074. 合并区间/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343

4444
**方法一:区间合并**
4545

46-
区间合并,将所有存在交集的区间进行合并。
46+
区间合并,将所有存在交集的区间进行合并。方法是:先对区间**按照左端点升序排列**,然后遍历区间进行合并。
4747

4848
模板:
4949

solution/0000-0099/0056.Merge Intervals/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141

4242
**方法一:区间合并**
4343

44-
区间合并,将所有存在交集的区间进行合并。
44+
区间合并,将所有存在交集的区间进行合并。方法是:先对区间**按照左端点升序排列**,然后遍历区间进行合并。
4545

4646
模板:
4747

solution/0000-0099/0057.Insert Interval/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666

6767
**方法一:区间合并**
6868

69-
区间合并,将所有存在交集的区间进行合并。
69+
区间合并,将所有存在交集的区间进行合并。方法是:先对区间**按照左端点升序排列**,然后遍历区间进行合并。
7070

7171
<!-- tabs:start -->
7272

solution/0000-0099/0057.Insert Interval/Solution.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
class Solution:
2-
def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
2+
def insert(
3+
self, intervals: List[List[int]], newInterval: List[int]
4+
) -> List[List[int]]:
35
def merge(intervals):
46
intervals.sort()
57
ans = []

solution/0300-0399/0303.Range Sum Query - Immutable/Solution.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class NumArray:
2-
32
def __init__(self, nums: List[int]):
43
self.s = [0] + list(accumulate(nums))
54

solution/0900-0999/0986.Interval List Intersections/README.md

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161

6262
<!-- 这里可写通用的实现逻辑 -->
6363

64-
双指针实现区间合并。
64+
**方法一:双指针**
6565

6666
<!-- tabs:start -->
6767

@@ -72,18 +72,18 @@
7272
```python
7373
class Solution:
7474
def intervalIntersection(self, firstList: List[List[int]], secondList: List[List[int]]) -> List[List[int]]:
75-
i = j = 0
76-
res = []
75+
i = j = 0
76+
ans = []
7777
while i < len(firstList) and j < len(secondList):
78-
l, r = max(firstList[i][0], secondList[j][0]), min(
79-
firstList[i][1], secondList[j][1])
78+
s1, e1, s2, e2 = *firstList[i], *secondList[j]
79+
l, r = max(s1, s2), min(e1, e2)
8080
if l <= r:
81-
res.append([l, r])
82-
if firstList[i][1] < secondList[j][1]:
81+
ans.append([l, r])
82+
if e1 < e2:
8383
i += 1
8484
else:
8585
j += 1
86-
return res
86+
return ans
8787
```
8888

8989
### **Java**
@@ -93,20 +93,21 @@ class Solution:
9393
```java
9494
class Solution {
9595
public int[][] intervalIntersection(int[][] firstList, int[][] secondList) {
96-
List<int[]> res = new ArrayList<>();
97-
for (int i = 0, j = 0; i < firstList.length && j < secondList.length;) {
96+
List<int[]> ans = new ArrayList<>();
97+
int m = firstList.length, n = secondList.length;
98+
for (int i = 0, j = 0; i < m && j < n;) {
9899
int l = Math.max(firstList[i][0], secondList[j][0]);
99100
int r = Math.min(firstList[i][1], secondList[j][1]);
100101
if (l <= r) {
101-
res.add(new int[]{l, r});
102+
ans.add(new int[]{l, r});
102103
}
103104
if (firstList[i][1] < secondList[j][1]) {
104105
++i;
105106
} else {
106107
++j;
107108
}
108109
}
109-
return res.toArray(new int[res.size()][]);
110+
return ans.toArray(new int[ans.size()][]);
110111
}
111112
}
112113
```
@@ -116,20 +117,18 @@ class Solution {
116117
```cpp
117118
class Solution {
118119
public:
119-
vector<vector<int>> intervalIntersection(vector<vector<int>> &firstList, vector<vector<int>> &secondList) {
120-
vector<vector<int>> res;
121-
for (int i = 0, j = 0; i < firstList.size() && j < secondList.size();)
120+
vector<vector<int>> intervalIntersection(vector<vector<int>>& firstList, vector<vector<int>>& secondList) {
121+
vector<vector<int>> ans;
122+
int m = firstList.size(), n = secondList.size();
123+
for (int i = 0, j = 0; i < m && j < n;)
122124
{
123125
int l = max(firstList[i][0], secondList[j][0]);
124126
int r = min(firstList[i][1], secondList[j][1]);
125-
if (l <= r)
126-
res.push_back({l, r});
127-
if (firstList[i][1] < secondList[j][1])
128-
++i;
129-
else
130-
++j;
127+
if (l <= r) ans.push_back({l, r});
128+
if (firstList[i][1] < secondList[j][1]) ++i;
129+
else ++j;
131130
}
132-
return res;
131+
return ans;
133132
}
134133
};
135134
```
@@ -138,20 +137,21 @@ public:
138137
139138
```go
140139
func intervalIntersection(firstList [][]int, secondList [][]int) [][]int {
141-
i, j := 0, 0
142-
var res [][]int
143-
for i < len(firstList) && j < len(secondList) {
144-
l, r := max(firstList[i][0], secondList[j][0]), min(firstList[i][1], secondList[j][1])
140+
m, n := len(firstList), len(secondList)
141+
var ans [][]int
142+
for i, j := 0, 0; i < m && j < n; {
143+
l := max(firstList[i][0], secondList[j][0])
144+
r := min(firstList[i][1], secondList[j][1])
145145
if l <= r {
146-
res = append(res, []int{l, r})
146+
ans = append(ans, []int{l, r})
147147
}
148148
if firstList[i][1] < secondList[j][1] {
149149
i++
150150
} else {
151151
j++
152152
}
153153
}
154-
return res
154+
return ans
155155
}
156156
157157
func max(a, b int) int {

solution/0900-0999/0986.Interval List Intersections/README_EN.md

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -48,39 +48,40 @@
4848
```python
4949
class Solution:
5050
def intervalIntersection(self, firstList: List[List[int]], secondList: List[List[int]]) -> List[List[int]]:
51-
i = j = 0
52-
res = []
51+
i = j = 0
52+
ans = []
5353
while i < len(firstList) and j < len(secondList):
54-
l, r = max(firstList[i][0], secondList[j][0]), min(
55-
firstList[i][1], secondList[j][1])
54+
s1, e1, s2, e2 = *firstList[i], *secondList[j]
55+
l, r = max(s1, s2), min(e1, e2)
5656
if l <= r:
57-
res.append([l, r])
58-
if firstList[i][1] < secondList[j][1]:
57+
ans.append([l, r])
58+
if e1 < e2:
5959
i += 1
6060
else:
6161
j += 1
62-
return res
62+
return ans
6363
```
6464

6565
### **Java**
6666

6767
```java
6868
class Solution {
6969
public int[][] intervalIntersection(int[][] firstList, int[][] secondList) {
70-
List<int[]> res = new ArrayList<>();
71-
for (int i = 0, j = 0; i < firstList.length && j < secondList.length;) {
70+
List<int[]> ans = new ArrayList<>();
71+
int m = firstList.length, n = secondList.length;
72+
for (int i = 0, j = 0; i < m && j < n;) {
7273
int l = Math.max(firstList[i][0], secondList[j][0]);
7374
int r = Math.min(firstList[i][1], secondList[j][1]);
7475
if (l <= r) {
75-
res.add(new int[]{l, r});
76+
ans.add(new int[]{l, r});
7677
}
7778
if (firstList[i][1] < secondList[j][1]) {
7879
++i;
7980
} else {
8081
++j;
8182
}
8283
}
83-
return res.toArray(new int[res.size()][]);
84+
return ans.toArray(new int[ans.size()][]);
8485
}
8586
}
8687
```
@@ -90,20 +91,18 @@ class Solution {
9091
```cpp
9192
class Solution {
9293
public:
93-
vector<vector<int>> intervalIntersection(vector<vector<int>> &firstList, vector<vector<int>> &secondList) {
94-
vector<vector<int>> res;
95-
for (int i = 0, j = 0; i < firstList.size() && j < secondList.size();)
94+
vector<vector<int>> intervalIntersection(vector<vector<int>>& firstList, vector<vector<int>>& secondList) {
95+
vector<vector<int>> ans;
96+
int m = firstList.size(), n = secondList.size();
97+
for (int i = 0, j = 0; i < m && j < n;)
9698
{
9799
int l = max(firstList[i][0], secondList[j][0]);
98100
int r = min(firstList[i][1], secondList[j][1]);
99-
if (l <= r)
100-
res.push_back({l, r});
101-
if (firstList[i][1] < secondList[j][1])
102-
++i;
103-
else
104-
++j;
101+
if (l <= r) ans.push_back({l, r});
102+
if (firstList[i][1] < secondList[j][1]) ++i;
103+
else ++j;
105104
}
106-
return res;
105+
return ans;
107106
}
108107
};
109108
```
@@ -112,20 +111,21 @@ public:
112111
113112
```go
114113
func intervalIntersection(firstList [][]int, secondList [][]int) [][]int {
115-
i, j := 0, 0
116-
var res [][]int
117-
for i < len(firstList) && j < len(secondList) {
118-
l, r := max(firstList[i][0], secondList[j][0]), min(firstList[i][1], secondList[j][1])
114+
m, n := len(firstList), len(secondList)
115+
var ans [][]int
116+
for i, j := 0, 0; i < m && j < n; {
117+
l := max(firstList[i][0], secondList[j][0])
118+
r := min(firstList[i][1], secondList[j][1])
119119
if l <= r {
120-
res = append(res, []int{l, r})
120+
ans = append(ans, []int{l, r})
121121
}
122122
if firstList[i][1] < secondList[j][1] {
123123
i++
124124
} else {
125125
j++
126126
}
127127
}
128-
return res
128+
return ans
129129
}
130130
131131
func max(a, b int) int {
Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
class Solution {
22
public:
3-
vector<vector<int>> intervalIntersection(vector<vector<int>> &firstList, vector<vector<int>> &secondList) {
4-
vector<vector<int>> res;
5-
for (int i = 0, j = 0; i < firstList.size() && j < secondList.size();)
3+
vector<vector<int>> intervalIntersection(vector<vector<int>>& firstList, vector<vector<int>>& secondList) {
4+
vector<vector<int>> ans;
5+
int m = firstList.size(), n = secondList.size();
6+
for (int i = 0, j = 0; i < m && j < n;)
67
{
78
int l = max(firstList[i][0], secondList[j][0]);
89
int r = min(firstList[i][1], secondList[j][1]);
9-
if (l <= r)
10-
res.push_back({l, r});
11-
if (firstList[i][1] < secondList[j][1])
12-
++i;
13-
else
14-
++j;
10+
if (l <= r) ans.push_back({l, r});
11+
if (firstList[i][1] < secondList[j][1]) ++i;
12+
else ++j;
1513
}
16-
return res;
14+
return ans;
1715
}
1816
};

solution/0900-0999/0986.Interval List Intersections/Solution.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
func intervalIntersection(firstList [][]int, secondList [][]int) [][]int {
2-
i, j := 0, 0
3-
var res [][]int
4-
for i < len(firstList) && j < len(secondList) {
5-
l, r := max(firstList[i][0], secondList[j][0]), min(firstList[i][1], secondList[j][1])
2+
m, n := len(firstList), len(secondList)
3+
var ans [][]int
4+
for i, j := 0, 0; i < m && j < n; {
5+
l := max(firstList[i][0], secondList[j][0])
6+
r := min(firstList[i][1], secondList[j][1])
67
if l <= r {
7-
res = append(res, []int{l, r})
8+
ans = append(ans, []int{l, r})
89
}
910
if firstList[i][1] < secondList[j][1] {
1011
i++
1112
} else {
1213
j++
1314
}
1415
}
15-
return res
16+
return ans
1617
}
1718

1819
func max(a, b int) int {
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
class Solution {
22
public int[][] intervalIntersection(int[][] firstList, int[][] secondList) {
3-
List<int[]> res = new ArrayList<>();
4-
for (int i = 0, j = 0; i < firstList.length && j < secondList.length;) {
3+
List<int[]> ans = new ArrayList<>();
4+
int m = firstList.length, n = secondList.length;
5+
for (int i = 0, j = 0; i < m && j < n;) {
56
int l = Math.max(firstList[i][0], secondList[j][0]);
67
int r = Math.min(firstList[i][1], secondList[j][1]);
78
if (l <= r) {
8-
res.add(new int[]{l, r});
9+
ans.add(new int[]{l, r});
910
}
1011
if (firstList[i][1] < secondList[j][1]) {
1112
++i;
1213
} else {
1314
++j;
1415
}
1516
}
16-
return res.toArray(new int[res.size()][]);
17+
return ans.toArray(new int[ans.size()][]);
1718
}
1819
}

0 commit comments

Comments
 (0)