归档

228. 汇总区间

228. 汇总区间

题目

描述:

给定一个 无重复元素 的 有序 整数数组 nums 。

区间 [a,b] 是从 a 到 b (包含) 的所有整数的集合。

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

列表中的每个区间范围 [a,b] 应该按如下格式输出:

  • "a->b" ,如果 a != b
  • "a" ,如果 a == b

示例 1:

  • 输入: nums = [0,1,2,4,5,7]
  • 输出: ["0->2","4->5","7"]
  • 解释: 区间范围是:
    • [0,2] --> "0->2"
    • [4,5] --> "4->5"
    • [7,7] --> "7"

示例 2:

  • 输入: nums = [0,2,3,4,6,8,9]
  • 输出: ["0","2->4","6","8->9"]
  • 解释: 区间范围是:
    • [0,0] --> "0"
    • [2,4] --> "2->4"
    • [6,6] --> "6"
    • [8,9] --> "8->9"

提示:

  • 0 <= nums.length <= 20
  • -2^31 <= nums[i] <= 2^31 - 1
  • nums 中的所有值都 互不相同
  • nums 按 升序 排列

题解

找出每一段连续整数的首尾

数组有序且没有重复元素,所以连续的整数一定相邻出现。顺着数组往后走,找出每一段连续区间的起点和终点,再按题目格式拼成字符串即可。

区间不能包含数组中不存在的整数,因此遇到数值间隙就必须结束当前段。例如 2 后面直接出现 4 时,不能把二者写成 2->4,否则会包含不存在的 3。每段尽可能延伸到连续关系中断,也就得到了所需的最少区间数量。

用指针 i 从左往右扫描。low 记录当前区间的起点下标,然后 i 继续往后扩:只要 nums[i] == nums[i - 1] + 1,这一段就还没断;条件不满足时,当前区间结束,终点下标 high = i - 1。输出时,low == high 写成单个数字,否则写成 "start->end"。

以 nums = [0,1,2,4,5,7] 为例:从 0 扩到 2,得到 "0->2";从 4 扩到 5,得到 "4->5";7 单独成段,得到 "7"。

代码是“外层找起点、内层往后扩”的双层循环,但两层共用同一个 i,它只向前移动,每个元素只扫一次,总时间仍是线性的。

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public List<String> summaryRanges(int[] nums) {
    List<String> result = new ArrayList<>();
    int n = nums.length;
    int i = 0;

    while (i < n) {
        int low = i;
        i++;

        while (i < n && nums[i] == nums[i - 1] + 1) {
            i++;
        }

        int high = i - 1;

        StringBuilder sb = new StringBuilder();
        sb.append(nums[low]);
        if (low < high) {
            sb.append("->");
            sb.append(nums[high]);
        }

        result.add(sb.toString());
    }

    return result;
}

时间复杂度是 O(n),因为整个数组只会被顺着扫一遍;空间复杂度如果不算结果列表本身,就是 O(1)。

空数组不进入循环,返回空列表;一个元素对应单值字符串。连续性比较采用“前值加一”,本题严格递增且无重复,只有存在后一个元素时才会执行比较,此时前值不可能是 Integer.MAX_VALUE,所以这里不会发生加一溢出。

完整示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {

    public static List<String> summaryRanges(int[] nums) {
        List<String> result = new ArrayList<>();
        int n = nums.length;
        int i = 0;

        while (i < n) {
            int low = i;
            i++;

            while (i < n && nums[i] == nums[i - 1] + 1) {
                i++;
            }

            int high = i - 1;

            StringBuilder sb = new StringBuilder();
            sb.append(nums[low]);
            if (low < high) {
                sb.append("->");
                sb.append(nums[high]);
            }

            result.add(sb.toString());
        }

        return result;
    }

    public static void main(String[] args) {
        int[] nums1 = {0, 1, 2, 4, 5, 7};
        System.out.println("示例 1 输入: " + Arrays.toString(nums1));
        List<String> res1 = summaryRanges(nums1);
        System.out.println("示例 1 输出: " + res1);
        System.out.println("-----------------------------");

        int[] nums2 = {0, 2, 3, 4, 6, 8, 9};
        System.out.println("示例 2 输入: " + Arrays.toString(nums2));
        List<String> res2 = summaryRanges(nums2);
        System.out.println("示例 2 输出: " + res2);
        System.out.println("-----------------------------");

        int[] nums3 = {};
        System.out.println("示例 3 输入: " + Arrays.toString(nums3));
        List<String> res3 = summaryRanges(nums3);
        System.out.println("示例 3 输出: " + res3);
        System.out.println("-----------------------------");

        int[] nums4 = {-1};
        System.out.println("示例 4 输入: " + Arrays.toString(nums4));
        List<String> res4 = summaryRanges(nums4);
        System.out.println("示例 4 输出: " + res4);
    }
}

运行结果(OpenJDK 21.0.12):

1
2
3
4
5
6
7
8
9
10
11
示例 1 输入: [0, 1, 2, 4, 5, 7]
示例 1 输出: [0->2, 4->5, 7]
-----------------------------
示例 2 输入: [0, 2, 3, 4, 6, 8, 9]
示例 2 输出: [0, 2->4, 6, 8->9]
-----------------------------
示例 3 输入: []
示例 3 输出: []
-----------------------------
示例 4 输入: [-1]
示例 4 输出: [-1]
本文由作者按照 CC BY 4.0 进行授权