|
| 1 | +# [352. Data Stream as Disjoint Intervals](https://leetcode.com/problems/data-stream-as-disjoint-intervals/) |
| 2 | + |
| 3 | + |
| 4 | +## 题目 |
| 5 | + |
| 6 | +Given a data stream input of non-negative integers a1, a2, ..., an, summarize the numbers seen so far as a list of disjoint intervals. |
| 7 | + |
| 8 | +Implement the SummaryRanges class: |
| 9 | + |
| 10 | + - SummaryRanges() Initializes the object with an empty stream. |
| 11 | + - void addNum(int val) Adds the integer val to the stream. |
| 12 | + - int[][] getIntervals() Returns a summary of the integers in the stream currently as a list of disjoint intervals [starti, endi]. |
| 13 | + |
| 14 | +**Example 1:** |
| 15 | + |
| 16 | + Input |
| 17 | + ["SummaryRanges", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals"] |
| 18 | + [[], [1], [], [3], [], [7], [], [2], [], [6], []] |
| 19 | + Output |
| 20 | + [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]]] |
| 21 | + |
| 22 | + Explanation |
| 23 | + SummaryRanges summaryRanges = new SummaryRanges(); |
| 24 | + summaryRanges.addNum(1); // arr = [1] |
| 25 | + summaryRanges.getIntervals(); // return [[1, 1]] |
| 26 | + summaryRanges.addNum(3); // arr = [1, 3] |
| 27 | + summaryRanges.getIntervals(); // return [[1, 1], [3, 3]] |
| 28 | + summaryRanges.addNum(7); // arr = [1, 3, 7] |
| 29 | + summaryRanges.getIntervals(); // return [[1, 1], [3, 3], [7, 7]] |
| 30 | + summaryRanges.addNum(2); // arr = [1, 2, 3, 7] |
| 31 | + summaryRanges.getIntervals(); // return [[1, 3], [7, 7]] |
| 32 | + summaryRanges.addNum(6); // arr = [1, 2, 3, 6, 7] |
| 33 | + summaryRanges.getIntervals(); // return [[1, 3], [6, 7]] |
| 34 | + |
| 35 | +**Constraints** |
| 36 | + |
| 37 | + - 0 <= val <= 10000 |
| 38 | + - At most 3 * 10000 calls will be made to addNum and getIntervals. |
| 39 | + |
| 40 | +## 题目大意 |
| 41 | + |
| 42 | +给你一个由非负整数a1, a2, ..., an 组成的数据流输入,请你将到目前为止看到的数字总结为不相交的区间列表。 |
| 43 | + |
| 44 | +实现 SummaryRanges 类: |
| 45 | + |
| 46 | + - SummaryRanges() 使用一个空数据流初始化对象。 |
| 47 | + - void addNum(int val) 向数据流中加入整数 val 。 |
| 48 | + - int[][] getIntervals() 以不相交区间[starti, endi] 的列表形式返回对数据流中整数的总结 |
| 49 | + |
| 50 | +## 解题思路 |
| 51 | + |
| 52 | +- 使用字典过滤掉重复的数字 |
| 53 | +- 把过滤后的数字放到nums中,并进行排序 |
| 54 | +- 使用nums构建不重复的区间 |
| 55 | + |
| 56 | +## 代码 |
| 57 | + |
| 58 | +```go |
| 59 | +package leetcode |
| 60 | + |
| 61 | +import "sort" |
| 62 | + |
| 63 | +type SummaryRanges struct { |
| 64 | + nums []int |
| 65 | + mp map[int]int |
| 66 | +} |
| 67 | + |
| 68 | +func Constructor() SummaryRanges { |
| 69 | + return SummaryRanges{ |
| 70 | + nums: []int{}, |
| 71 | + mp : map[int]int{}, |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func (this *SummaryRanges) AddNum(val int) { |
| 76 | + if _, ok := this.mp[val]; !ok { |
| 77 | + this.mp[val] = 1 |
| 78 | + this.nums = append(this.nums, val) |
| 79 | + } |
| 80 | + sort.Ints(this.nums) |
| 81 | +} |
| 82 | + |
| 83 | +func (this *SummaryRanges) GetIntervals() [][]int { |
| 84 | + n := len(this.nums) |
| 85 | + var ans [][]int |
| 86 | + if n == 0 { |
| 87 | + return ans |
| 88 | + } |
| 89 | + if n == 1 { |
| 90 | + ans = append(ans, []int{this.nums[0], this.nums[0]}) |
| 91 | + return ans |
| 92 | + } |
| 93 | + start, end := this.nums[0], this.nums[0] |
| 94 | + ans = append(ans, []int{start, end}) |
| 95 | + index := 0 |
| 96 | + for i := 1; i < n; i++ { |
| 97 | + if this.nums[i] == end + 1 { |
| 98 | + end = this.nums[i] |
| 99 | + ans[index][1] = end |
| 100 | + } else { |
| 101 | + start, end = this.nums[i], this.nums[i] |
| 102 | + ans = append(ans, []int{start, end}) |
| 103 | + index++ |
| 104 | + } |
| 105 | + } |
| 106 | + return ans |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | + |
| 111 | +---------------------------------------------- |
| 112 | +<div style="display: flex;justify-content: space-between;align-items: center;"> |
| 113 | +<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0350.Intersection-of-Two-Arrays-II/">⬅️上一页</a></p> |
| 114 | +<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0354.Russian-Doll-Envelopes/">下一页➡️</a></p> |
| 115 | +</div> |
0 commit comments