# 第五十周ARTS总结

# Algorithm

1ms | 98.49% Run time
41.4MB | 75.00% Memory

public int[][] insert(int[][] intervals, int[] newInterval) {
    List<int[]> ans = new ArrayList<>();

    Integer left = null;// 区间左
    Integer right = null;// 区间右
    boolean isFind = false;// 是否找到合并后的区间

    // 遍历每个区间,找到newInterval的区间范围
    for (int[] temp : intervals) {
        // 还未找到左,开始找左
        if (left == null) {
            // 判断newInterval[0]的位置
            if (newInterval[0] < temp[0]) {
                left = newInterval[0];
            } else if (newInterval[0] >= temp[0] && newInterval[0] <= temp[1]) {
                left = temp[0];
            } else {
                ans.add(temp);
            }
        }

        // 已经找到左了,开始找右
        if (left != null && !isFind) {
            // 判断newInterval[1]的位置
            if (newInterval[1] > temp[1]) {
                right = newInterval[1];
            } else if (newInterval[1] >= temp[0] && newInterval[1] <= temp[1]) {
                right = temp[1];
                // 此时的右是事实上的右,因此可添加进ans
                ans.add(new int[]{left, right});

                isFind = true;
                continue;
            } else {
                right = newInterval[1];

                // 此时整个新区间都在最左边
                ans.add(new int[]{left, right});
                ans.add(temp);

                isFind = true;
                continue;
            }
        }

        // 左右都找到了,但是右有可能扩展
        if (left != null && right != null) {
            if (isFind) {
                ans.add(temp);
            } else {
                if (right < temp[0]) {
                    ans.add(new int[]{left, right});
                    isFind = true;

                    // 再加上本区间
                    ans.add(temp);
                } else if (right >= temp[0] && right <= temp[1]) {
                    right = temp[1];
                    ans.add(new int[]{left, right});
                    isFind = true;
                }
            }
        }
    }

    if (!isFind) {
        // 防止出现找了一圈没找到的情况
        if (left == null && newInterval.length > 0) {
            ans.add(newInterval);
        } else if (left != null) {
            ans.add(new int[]{left, right});
        }
        isFind = true;
    }

    int[][] realAns = new int[ans.size()][];
    ans.toArray(realAns);

    return realAns;
}
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

# Review

# Tip

  • SQLite数据库一般存储在/data/data/<package name>/databases/目录下
  • 只有调用SQLiteOpenHelper#getReadableDatabaseSQLiteOpenHelper#getWritableDatabase时才会创建数据库,单纯的实例化SQLiteOpenHelper并不会创建数据库
  • SQLiteOpenHelper#getReadableDatabaseSQLiteOpenHelper#getWritableDatabase没有本质区别,只有当磁盘已满的情况下才会有区别
  • 判断是否有权限:ActivityCompat#checkSelfPermission
  • 申请权限:ActivityCompat#requestPermissions
  • 申请权限回调:onRequestPermissionsResult

# Share

暂无内容

更新时间: 10/20/2022, 7:04:01 AM