Skip to content

Commit 7a1907f

Browse files
committed
一刷228
1 parent 043214d commit 7a1907f

File tree

7 files changed

+112
-25
lines changed

7 files changed

+112
-25
lines changed

README.adoc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1622,13 +1622,13 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
16221622
|Medium
16231623
|
16241624

1625-
//|{counter:codes}
1626-
//|{leetcode_base_url}/summary-ranges/[228. Summary Ranges^]
1627-
//|{source_base_url}/_0228_SummaryRanges.java[Java]
1628-
//|{doc_base_url}/0228-summary-ranges.adoc[题解]
1629-
//|Medium
1630-
//|
1631-
//
1625+
|{counter:codes}
1626+
|{leetcode_base_url}/summary-ranges/[228. Summary Ranges^]
1627+
|{source_base_url}/_0228_SummaryRanges.java[Java]
1628+
|{doc_base_url}/0228-summary-ranges.adoc[题解]
1629+
|Medium
1630+
|
1631+
16321632
//|{counter:codes}
16331633
//|{leetcode_base_url}/majority-element-ii/[229. Majority Element II^]
16341634
//|{source_base_url}/_0229_MajorityElementII.java[Java]

docs/0000-06-two-pointer.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
. xref:0186-reverse-words-in-a-string-ii.adoc[186. Reverse Words in a String II]
4444
. xref:0189-rotate-array.adoc[189. Rotate Array]
4545
. xref:0202-happy-number.adoc[202. Happy Number]
46+
. xref:0228-summary-ranges.adoc[228. Summary Ranges]
4647
. xref:0234-palindrome-linked-list.adoc[234. Palindrome Linked List]
4748
. xref:0244-shortest-word-distance-ii.adoc[244. Shortest Word Distance II]
4849
. xref:0246-strobogrammatic-number.adoc[246. Strobogrammatic Number]

docs/0228-summary-ranges.adoc

Lines changed: 61 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,77 @@
11
[#0228-summary-ranges]
2-
= 228. Summary Ranges
2+
= 228. 汇总区间
33

4-
{leetcode}/problems/summary-ranges/[LeetCode - Summary Ranges^]
4+
https://leetcode.cn/problems/summary-ranges/[LeetCode - 228. 汇总区间 ^]
55

6-
Given a sorted integer array without duplicates, return the summary of its ranges.
6+
给定一个 *无重复元素**有序* 整数数组 `nums`
77

8-
*Example 1:*
8+
返回 _**恰好覆盖数组中所有数字** 的 *最小有序* 区间范围列表_。也就是说,`nums` 的每个元素都恰好被某个区间范围所覆盖,并且不存在属于某个范围但不属于 `nums` 的数字 `x`
99

10-
[subs="verbatim,quotes,macros"]
11-
----
12-
*Input:* [0,1,2,4,5,7]
13-
*Output:* ["0->2","4->5","7"]
14-
*Explanation:* 0,1,2 form a continuous range; 4,5 form a continuous range.
15-
----
10+
列表中的每个区间范围 `[a,b]` 应该按如下格式输出:
1611

17-
*Example 2:*
12+
* `+a->b+` ,如果 `+a != b+`
13+
* `a` ,如果 `a == b`
1814
19-
[subs="verbatim,quotes,macros"]
20-
----
21-
*Input:* [0,2,3,4,6,8,9]
22-
*Output:* ["0","2->4","6","8->9"]
23-
*Explanation:* 2,3,4 form a continuous range; 8,9 form a continuous range.
24-
----
15+
*示例 1:*
16+
17+
....
18+
输入:nums = [0,1,2,4,5,7]
19+
输出:["0->2","4->5","7"]
20+
解释:区间范围是:
21+
[0,2] --> "0->2"
22+
[4,5] --> "4->5"
23+
[7,7] --> "7"
24+
....
25+
26+
*示例 2:*
27+
28+
....
29+
输入:nums = [0,2,3,4,6,8,9]
30+
输出:["0","2->4","6","8->9"]
31+
解释:区间范围是:
32+
[0,0] --> "0"
33+
[2,4] --> "2->4"
34+
[6,6] --> "6"
35+
[8,9] --> "8->9"
36+
....
2537

38+
*提示:*
2639

40+
* `+0 <= nums.length <= 20+`
41+
* `-2^31^ \<= nums[i] \<= 2^31^ - 1`
42+
* `nums` 中的所有值都 *互不相同*
43+
* `nums` 按升序排列
44+
45+
46+
== 思路分析
47+
48+
双指针:如果当前元素与下一个元素相差 `1`,则继续向右推进,否则要么单独加入结果集,要么和起始节点的数字组成区间加入结果集。
2749

2850
[[src-0228]]
51+
[tabs]
52+
====
53+
一刷::
54+
+
55+
--
2956
[{java_src_attr}]
3057
----
3158
include::{sourcedir}/_0228_SummaryRanges.java[tag=answer]
3259
----
60+
--
61+
62+
// 二刷::
63+
// +
64+
// --
65+
// [{java_src_attr}]
66+
// ----
67+
// include::{sourcedir}/_0228_SummaryRanges_2.java[tag=answer]
68+
// ----
69+
// --
70+
====
71+
72+
73+
== 参考资料
3374

75+
. https://leetcode.cn/problems/summary-ranges/solutions/553645/hui-zong-qu-jian-by-leetcode-solution-6zrs/[228. 汇总区间 - 官方题解^]
76+
. https://leetcode.cn/problems/summary-ranges/solutions/2411792/python3javacgotypescript-yi-ti-yi-jie-sh-gpep/[228. 汇总区间 - 一题一解:双指针(清晰题解)^]
77+
. https://leetcode.cn/problems/summary-ranges/solutions/554334/shun-xu-dao-xu-hua-chuang-dan-bian-liang-gl8j/[228. 汇总区间 - 顺序 + 倒序 + 滑窗 + 单变量(4解法,超98%)^]

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ include::0226-invert-binary-tree.adoc[leveloffset=+1]
539539

540540
include::0227-basic-calculator-ii.adoc[leveloffset=+1]
541541

542-
// include::0228-summary-ranges.adoc[leveloffset=+1]
542+
include::0228-summary-ranges.adoc[leveloffset=+1]
543543

544544
// include::0229-majority-element-ii.adoc[leveloffset=+1]
545545

logbook/202503.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,10 @@ endif::[]
973973
|{doc_base_url}/0219-contains-duplicate-ii.adoc[题解]
974974
|✅ 使用哈希存每个数字的最后坐标,遇到相同就判断是否满足条件,否则继续推进。看题解,只保存滑动窗口范围内的数字,可以更节省内存!
975975

976+
|{counter:codes2503}
977+
|{leetcode_base_url}/summary-ranges/[228. 汇总区间^]
978+
|{doc_base_url}/0228-summary-ranges.adoc[题解]
979+
|✅ 双指针:一个指针记录起始位置,一个指针向前推进,有起始则区间加入,没有则单独加入。
976980

977981
|===
978982

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ public class _0227_BasicCalculatorIi {
4848
* Memory Usage: 42.8 MB, less than 5.97% of Java online submissions for Basic Calculator II.
4949
*
5050
* Copy from: https://leetcode.com/problems/basic-calculator-ii/discuss/62996/Java-straight-forward-iteration-Solution-with-comments-No-Stack-O(N)-and-O(1)[Java straight forward iteration Solution with comments, No Stack, O(N) & O(1) - LeetCode Discuss]
51+
*
52+
* @author D瓜哥 · https://www.diguage.com
53+
* @since 2020-01-19 23:30
5154
*/
5255
public int calculate(String s) {
5356
if (Objects.isNull(s) || s.length() == 0) {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class _0228_SummaryRanges {
7+
// tag::answer[]
8+
/**
9+
* @author D瓜哥 · https://www.diguage.com
10+
* @since 2025-06-14 21:50:13
11+
*/
12+
public List<String> summaryRanges(int[] nums) {
13+
List<String> result = new ArrayList<>();
14+
int start = -1;
15+
for (int i = 0; i < nums.length; i++) {
16+
if (i < nums.length - 1 && nums[i] + 1 == nums[i + 1]) {
17+
if (start == -1) {
18+
start = i;
19+
}
20+
} else {
21+
if (start == -1) {
22+
result.add(String.valueOf(nums[i]));
23+
} else {
24+
result.add(nums[start] + "->" + nums[i]);
25+
start = -1;
26+
}
27+
}
28+
}
29+
return result;
30+
}
31+
// end::answer[]
32+
public static void main(String[] args) {
33+
new _0228_SummaryRanges().summaryRanges(new int[]{0, 2, 3, 4, 6, 8, 9});
34+
}
35+
}

0 commit comments

Comments
 (0)