mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
45 lines
2.1 KiB
HTML
45 lines
2.1 KiB
HTML
<p>Given a data stream input of non-negative integers <code>a<sub>1</sub>, a<sub>2</sub>, ..., a<sub>n</sub></code>, summarize the numbers seen so far as a list of disjoint intervals.</p>
|
|
|
|
<p>Implement the <code>SummaryRanges</code> class:</p>
|
|
|
|
<ul>
|
|
<li><code>SummaryRanges()</code> Initializes the object with an empty stream.</li>
|
|
<li><code>void addNum(int val)</code> Adds the integer <code>val</code> to the stream.</li>
|
|
<li><code>int[][] getIntervals()</code> Returns a summary of the integers in the stream currently as a list of disjoint intervals <code>[start<sub>i</sub>, end<sub>i</sub>]</code>.</li>
|
|
</ul>
|
|
|
|
<p> </p>
|
|
<p><strong>Example 1:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input</strong>
|
|
["SummaryRanges", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals"]
|
|
[[], [1], [], [3], [], [7], [], [2], [], [6], []]
|
|
<strong>Output</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>Explanation</strong>
|
|
SummaryRanges summaryRanges = new SummaryRanges();
|
|
summaryRanges.addNum(1); // arr = [1]
|
|
summaryRanges.getIntervals(); // return [[1, 1]]
|
|
summaryRanges.addNum(3); // arr = [1, 3]
|
|
summaryRanges.getIntervals(); // return [[1, 1], [3, 3]]
|
|
summaryRanges.addNum(7); // arr = [1, 3, 7]
|
|
summaryRanges.getIntervals(); // return [[1, 1], [3, 3], [7, 7]]
|
|
summaryRanges.addNum(2); // arr = [1, 2, 3, 7]
|
|
summaryRanges.getIntervals(); // return [[1, 3], [7, 7]]
|
|
summaryRanges.addNum(6); // arr = [1, 2, 3, 6, 7]
|
|
summaryRanges.getIntervals(); // return [[1, 3], [6, 7]]
|
|
</pre>
|
|
|
|
<p> </p>
|
|
<p><strong>Constraints:</strong></p>
|
|
|
|
<ul>
|
|
<li><code>0 <= val <= 10<sup>4</sup></code></li>
|
|
<li>At most <code>3 * 10<sup>4</sup></code> calls will be made to <code>addNum</code> and <code>getIntervals</code>.</li>
|
|
</ul>
|
|
|
|
<p> </p>
|
|
<p><strong>Follow up:</strong> What if there are lots of merges and the number of disjoint intervals is small compared to the size of the data stream?</p>
|