Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 928e91a

Browse files
committed
feat: add solutions to lc problem: No.0986
No.0986.Interval List Intersections
1 parent 6e8af70 commit 928e91a

File tree

7 files changed

+182
-21
lines changed

7 files changed

+182
-21
lines changed

‎solution/0200-0299/0278.First Bad Version/README.md‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,17 +193,17 @@ func firstBadVersion(n int) int {
193193

194194
impl Solution {
195195
pub fn first_bad_version(&self, n: i32) -> i32 {
196-
let mut l = 1;
197-
let mut r = n;
198-
while l<=r {
199-
let mid = l + (r - l) / 2;
196+
let mut left = 1;
197+
let mut right = n;
198+
while left<right {
199+
let mid = left + (right - left) / 2;
200200
if self.isBadVersion(mid) {
201-
r = mid-1
201+
right = mid;
202202
} else {
203-
l = mid + 1
203+
left = mid + 1;
204204
}
205205
}
206-
l
206+
left
207207
}
208208
}
209209
```

‎solution/0200-0299/0278.First Bad Version/README_EN.md‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -182,17 +182,17 @@ func firstBadVersion(n int) int {
182182

183183
impl Solution {
184184
pub fn first_bad_version(&self, n: i32) -> i32 {
185-
let mut l = 1;
186-
let mut r = n;
187-
while l<=r {
188-
let mid = l + (r - l) / 2;
185+
let mut left = 1;
186+
let mut right = n;
187+
while left<right {
188+
let mid = left + (right - left) / 2;
189189
if self.isBadVersion(mid) {
190-
r = mid-1
190+
right = mid;
191191
} else {
192-
l = mid + 1
192+
left = mid + 1;
193193
}
194194
}
195-
l
195+
left
196196
}
197197
}
198198
```

‎solution/0200-0299/0278.First Bad Version/Solution.rs‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44

55
impl Solution {
66
pub fn first_bad_version(&self, n: i32) -> i32 {
7-
let mut l = 1;
8-
let mut r = n;
9-
while l <= r {
10-
let mid = l + (r - l) / 2;
7+
let mut left = 1;
8+
let mut right = n;
9+
while left < right {
10+
let mid = left + (right - left) / 2;
1111
if self.isBadVersion(mid) {
12-
r = mid - 1
12+
right = mid;
1313
} else {
14-
l = mid + 1
14+
left = mid + 1;
1515
}
1616
}
17-
l
17+
left
1818
}
1919
}

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,63 @@ func min(a, b int) int {
169169
}
170170
```
171171

172+
### **TypeScript**
173+
174+
```ts
175+
function intervalIntersection(
176+
firstList: number[][],
177+
secondList: number[][],
178+
): number[][] {
179+
const n = firstList.length;
180+
const m = secondList.length;
181+
const res = [];
182+
let i = 0;
183+
let j = 0;
184+
while (i < n && j < m) {
185+
const start = Math.max(firstList[i][0], secondList[j][0]);
186+
const end = Math.min(firstList[i][1], secondList[j][1]);
187+
if (start <= end) {
188+
res.push([start, end]);
189+
}
190+
if (firstList[i][1] < secondList[j][1]) {
191+
i++;
192+
} else {
193+
j++;
194+
}
195+
}
196+
return res;
197+
}
198+
```
199+
200+
### **Rust**
201+
202+
```rust
203+
impl Solution {
204+
pub fn interval_intersection(
205+
first_list: Vec<Vec<i32>>,
206+
second_list: Vec<Vec<i32>>,
207+
) -> Vec<Vec<i32>> {
208+
let n = first_list.len();
209+
let m = second_list.len();
210+
let mut res = Vec::new();
211+
let (mut i, mut j) = (0, 0);
212+
while i < n && j < m {
213+
let start = first_list[i][0].max(second_list[j][0]);
214+
let end = first_list[i][1].min(second_list[j][1]);
215+
if start <= end {
216+
res.push(vec![start, end]);
217+
}
218+
if first_list[i][1] < second_list[j][1] {
219+
i += 1;
220+
} else {
221+
j += 1;
222+
}
223+
}
224+
res
225+
}
226+
}
227+
```
228+
172229
### **...**
173230

174231
```

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,63 @@ func min(a, b int) int {
143143
}
144144
```
145145

146+
### **TypeScript**
147+
148+
```ts
149+
function intervalIntersection(
150+
firstList: number[][],
151+
secondList: number[][],
152+
): number[][] {
153+
const n = firstList.length;
154+
const m = secondList.length;
155+
const res = [];
156+
let i = 0;
157+
let j = 0;
158+
while (i < n && j < m) {
159+
const start = Math.max(firstList[i][0], secondList[j][0]);
160+
const end = Math.min(firstList[i][1], secondList[j][1]);
161+
if (start <= end) {
162+
res.push([start, end]);
163+
}
164+
if (firstList[i][1] < secondList[j][1]) {
165+
i++;
166+
} else {
167+
j++;
168+
}
169+
}
170+
return res;
171+
}
172+
```
173+
174+
### **Rust**
175+
176+
```rust
177+
impl Solution {
178+
pub fn interval_intersection(
179+
first_list: Vec<Vec<i32>>,
180+
second_list: Vec<Vec<i32>>,
181+
) -> Vec<Vec<i32>> {
182+
let n = first_list.len();
183+
let m = second_list.len();
184+
let mut res = Vec::new();
185+
let (mut i, mut j) = (0, 0);
186+
while i < n && j < m {
187+
let start = first_list[i][0].max(second_list[j][0]);
188+
let end = first_list[i][1].min(second_list[j][1]);
189+
if start <= end {
190+
res.push(vec![start, end]);
191+
}
192+
if first_list[i][1] < second_list[j][1] {
193+
i += 1;
194+
} else {
195+
j += 1;
196+
}
197+
}
198+
res
199+
}
200+
}
201+
```
202+
146203
### **...**
147204

148205
```
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
impl Solution {
2+
pub fn interval_intersection(
3+
first_list: Vec<Vec<i32>>,
4+
second_list: Vec<Vec<i32>>,
5+
) -> Vec<Vec<i32>> {
6+
let n = first_list.len();
7+
let m = second_list.len();
8+
let mut res = Vec::new();
9+
let (mut i, mut j) = (0, 0);
10+
while i < n && j < m {
11+
let start = first_list[i][0].max(second_list[j][0]);
12+
let end = first_list[i][1].min(second_list[j][1]);
13+
if start <= end {
14+
res.push(vec![start, end]);
15+
}
16+
if first_list[i][1] < second_list[j][1] {
17+
i += 1;
18+
} else {
19+
j += 1;
20+
}
21+
}
22+
res
23+
}
24+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function intervalIntersection(
2+
firstList: number[][],
3+
secondList: number[][],
4+
): number[][] {
5+
const n = firstList.length;
6+
const m = secondList.length;
7+
const res = [];
8+
let i = 0;
9+
let j = 0;
10+
while (i < n && j < m) {
11+
const start = Math.max(firstList[i][0], secondList[j][0]);
12+
const end = Math.min(firstList[i][1], secondList[j][1]);
13+
if (start <= end) {
14+
res.push([start, end]);
15+
}
16+
if (firstList[i][1] < secondList[j][1]) {
17+
i++;
18+
} else {
19+
j++;
20+
}
21+
}
22+
return res;
23+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /