# LeetCode: 352. 将数据流变为多个不相交区间¶

## 1、题目描述¶

[1, 1]
[1, 1], [3, 3]
[1, 1], [3, 3], [7, 7]
[1, 3], [7, 7]
[1, 3], [6, 7]


## 2、解题思路¶

• 使用区间二分查找法

import bisect

class SummaryRanges:

def __init__(self):
"""
Initialize your data structure here.
"""
self.data = []

def addNum(self, val: int) -> None:
index = bisect.bisect_left(self.data, [val, val])
if index < len(self.data):
max_interval = self.data[index]
if index == 0:
if val == max_interval[0] - 1:
self.data[index][0] = val
elif val < max_interval[0] - 1:
self.data.insert(index, [val, val])
else:
min_interval = self.data[index - 1]
if min_interval[1] + 1 < val < max_interval[0] - 1:
self.data.insert(index, [val, val])
else:
if val == min_interval[1] + 1:
self.data[index - 1][1] = val
if val == max_interval[0] - 1:
self.data[index][0] = val
if self.data[index - 1][1] == self.data[index][0]:
self.data[index][0] = self.data[index - 1][0]
self.data.pop(index - 1)
else:
if not self.data or val > self.data[-1][1] + 1:

self.data.append([val, val])
elif val == self.data[-1][1] + 1:
self.data[-1][1] = val

def getIntervals(self) -> List[List[int]]:
return self.data

# Your SummaryRanges object will be instantiated and called as such:
# obj = SummaryRanges()