solution.md 3.6 KB
Newer Older
ToTensor's avatar
ToTensor 已提交
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
# 将数据流变为多个不相交区间

<p>&nbsp;给你一个由非负整数&nbsp;<code>a<sub>1</sub>, a<sub>2</sub>, ..., a<sub>n</sub></code> 组成的数据流输入,请你将到目前为止看到的数字总结为不相交的区间列表。</p>

<p>实现 <code>SummaryRanges</code> 类:</p>

<div class="original__bRMd">
<div>
<ul>
	<li><code>SummaryRanges()</code> 使用一个空数据流初始化对象。</li>
	<li><code>void addNum(int val)</code> 向数据流中加入整数 <code>val</code></li>
	<li><code>int[][] getIntervals()</code> 以不相交区间&nbsp;<code>[start<sub>i</sub>, end<sub>i</sub>]</code> 的列表形式返回对数据流中整数的总结。</li>
</ul>

<p>&nbsp;</p>

<p><strong>示例:</strong></p>

<pre>
<strong>输入:</strong>
["SummaryRanges", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals"]
[[], [1], [], [3], [], [7], [], [2], [], [6], []]
<strong>输出:</strong>
[null, null, [[1, 1]], null, [[1, 1], [3, 3]], null, [[1, 1], [3, 3], [7, 7]], null, [[1, 3], [7, 7]], null, [[1, 3], [6, 7]]]

<strong>解释:</strong>
SummaryRanges summaryRanges = new SummaryRanges();
summaryRanges.addNum(1);      // arr = [1]
summaryRanges.getIntervals(); // 返回 [[1, 1]]
summaryRanges.addNum(3);      // arr = [1, 3]
summaryRanges.getIntervals(); // 返回 [[1, 1], [3, 3]]
summaryRanges.addNum(7);      // arr = [1, 3, 7]
summaryRanges.getIntervals(); // 返回 [[1, 1], [3, 3], [7, 7]]
summaryRanges.addNum(2);      // arr = [1, 2, 3, 7]
summaryRanges.getIntervals(); // 返回 [[1, 3], [7, 7]]
summaryRanges.addNum(6);      // arr = [1, 2, 3, 6, 7]
summaryRanges.getIntervals(); // 返回 [[1, 3], [6, 7]]
</pre>

<p>&nbsp;</p>

<p><strong>提示:</strong></p>

<ul>
	<li><code>0 &lt;= val &lt;= 10<sup>4</sup></code></li>
	<li>最多调用&nbsp;<code>addNum</code><code>getIntervals</code> 方法 <code>3 * 10<sup>4</sup></code></li>
</ul>
</div>
</div>

<p>&nbsp;</p>

<p><strong>进阶:</strong>如果存在大量合并,并且与数据流的大小相比,不相交区间的数量很小,该怎么办?</p>


## template

```python

ToTensor's avatar
ToTensor 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
class SummaryRanges:
    def __init__(self):
        self.rec = set()

        self.s = list()

    def binarySearch_l(self, x: int):
        s = self.s
        l, r = 0, len(s) - 1
        while l < r:
            mid = l + r >> 1
            if s[mid][0] >= x:
                r = mid
            else:
                l = mid + 1
        return l

    def binarySearch(self, x: int):
        s = self.s
        l, r = 0, len(s) - 1
        while l < r:
            mid = l + r >> 1
            if s[mid][1] >= x:
                r = mid
            else:
                l = mid + 1
        return l

    def addNum(self, val: int) -> None:
        rec, s = self.rec, self.s
        if val in rec:
            return
        else:

            if val - 1 not in rec and val + 1 not in rec:

                s.append([val, val])
                s.sort()
            elif val - 1 in rec and val + 1 not in rec:

                p = self.binarySearch(val - 1)
                s[p][1] = val
            elif val - 1 not in rec and val + 1 in rec:

                p = self.binarySearch_l(val + 1)
                s[p][0] = val
            else:

                p = self.binarySearch(val)
                s[p - 1][1] = s[p][1]

                s.pop(p)
            rec.add(val)

    def getIntervals(self) -> List[List[int]]:
        return self.s
ToTensor's avatar
ToTensor 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143

```

## 答案

```python

```

## 选项

### A

```python

```

### B

```python

```

### C

```python

```