Skip to content

Commit cc9bc94

Browse files
committed
一刷57
1 parent e1a2b95 commit cc9bc94

File tree

7 files changed

+91
-8
lines changed

7 files changed

+91
-8
lines changed

README.adoc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -425,12 +425,12 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
425425
|Medium
426426
|
427427

428-
//|{counter:codes}
429-
//|{leetcode_base_url}/insert-interval/[57. Insert Interval^]
430-
//|{source_base_url}/_0057_InsertInterval.java[Java]
431-
//|{doc_base_url}/0057-insert-interval.adoc[题解]
432-
//|Hard
433-
//|
428+
|{counter:codes}
429+
|{leetcode_base_url}/insert-interval/[57. Insert Interval^]
430+
|{source_base_url}/_0057_InsertInterval.java[Java]
431+
|{doc_base_url}/0057-insert-interval.adoc[题解]
432+
|Hard
433+
|
434434

435435
|{counter:codes}
436436
|{leetcode_base_url}/length-of-last-word/[58. Length of Last Word^]

docs/0057-insert-interval.adoc

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,33 @@ You may assume that the intervals were initially sorted according to their start
2727

2828
*NOTE:* input types have been changed on April 15, 2019. Please reset to default code definition to get new method signature.
2929

30+
== 解题分析
31+
32+
image::images/0057-01.png[{image_attr}]
3033

3134
[[src-0057]]
35+
[tabs]
36+
====
37+
一刷::
38+
+
39+
--
3240
[{java_src_attr}]
3341
----
3442
include::{sourcedir}/_0057_InsertInterval.java[tag=answer]
3543
----
36-
44+
--
45+
46+
// 二刷::
47+
// +
48+
// --
49+
// [{java_src_attr}]
50+
// ----
51+
// include::{sourcedir}/_0057_InsertInterval_2.java[tag=answer]
52+
// ----
53+
// --
54+
====
55+
56+
== 参考资料
57+
58+
. https://leetcode.cn/problems/insert-interval/solutions/472151/cha-ru-qu-jian-by-leetcode-solution/[57. 插入区间 - 官方题解^]
59+
.

docs/images/0057-01.png

211 KB
Loading

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ include::0055-jump-game.adoc[leveloffset=+1]
188188

189189
include::0056-merge-intervals.adoc[leveloffset=+1]
190190

191-
// include::0057-insert-interval.adoc[leveloffset=+1]
191+
include::0057-insert-interval.adoc[leveloffset=+1]
192192

193193
include::0058-length-of-last-word.adoc[leveloffset=+1]
194194

logbook/202406.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,11 @@
409409
|{doc_base_url}/0744-find-smallest-letter-greater-than-target.adoc[题解]
410410
|二分查找取左右端点是一个怎样的套路?
411411

412+
|{counter:codes}
413+
|{leetcode_base_url}/insert-interval/[57. Insert Interval^]
414+
|{doc_base_url}/0057-insert-interval.adoc[题解]
415+
|合并区间,思路周全再写代码!
416+
412417
|===
413418

414419
截止目前,本轮练习一共完成 {codes} 道题。

src/main/java/com/diguage/algo/leetcode/_0056_MergeIntervals.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@
3636
*/
3737
public class _0056_MergeIntervals {
3838
// tag::answer[]
39+
40+
/**
41+
* @author D瓜哥 · https://www.diguage.com
42+
* @since 2019-10-23 12:27
43+
*/
3944
public int[][] merge(int[][] intervals) {
4045
if (Objects.isNull(intervals) || intervals.length <= 1) {
4146
return intervals;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
public class _0057_InsertInterval {
8+
// tag::answer[]
9+
10+
/**
11+
* @author D瓜哥 · https://www.diguage.com
12+
* @since 2019-10-23 12:27
13+
*/
14+
public int[][] insert(int[][] intervals, int[] newInterval) {
15+
int left = newInterval[0];
16+
int right = newInterval[1];
17+
boolean flag = false;
18+
List<int[]> result = new ArrayList<>();
19+
for (int[] ints : intervals) {
20+
if (right < ints[0]) {
21+
// 在插入区间的右侧且无交集
22+
if (!flag) {
23+
// 注意:这里是 left, right,不能直接放 newInterval
24+
result.add(new int[]{left, right});
25+
flag = true;
26+
}
27+
result.add(ints);
28+
} else if (ints[1] < left) {
29+
// 在插入区间的左侧且无交集
30+
result.add(ints);
31+
} else {
32+
left = Math.min(left, ints[0]);
33+
right = Math.max(right, ints[1]);
34+
}
35+
}
36+
if (!flag) {
37+
result.add(new int[]{left, right});
38+
}
39+
return result.toArray(new int[result.size()][2]);
40+
}
41+
42+
// end::answer[]
43+
public static void main(String[] args) {
44+
int[][] result = new _0057_InsertInterval().insert(
45+
new int[][]{new int[]{1, 2}, new int[]{3, 5},
46+
new int[]{6, 7}, new int[]{8, 10}, new int[]{12, 16}},
47+
new int[]{4, 8});
48+
System.out.println(Arrays.toString(result));
49+
}
50+
}

0 commit comments

Comments
 (0)