★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-oqbplbfs-me.html
➤若是连接不是山青咏芝的博客园地址,则多是爬取做者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持做者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
A Range Module is a module that tracks ranges of numbers. Your task is to design and implement the following interfaces in an efficient manner.git
addRange(int left, int right)
Adds the half-open interval [left, right)
, tracking every real number in that interval. Adding an interval that partially overlaps with currently tracked numbers should add any numbers in the interval [left, right)
that are not already tracked.queryRange(int left, int right)
Returns true if and only if every real number in the interval [left, right)
is currently being tracked. removeRange(int left, int right)
Stops tracking every real number currently being tracked in the interval [left, right)
.Example 1:github
addRange(10, 20): null removeRange(14, 16): null queryRange(10, 14): true (Every number in [10, 14) is being tracked) queryRange(13, 15): false (Numbers like 14, 14.03, 14.17 in [13, 15) are not being tracked) queryRange(16, 17): true (The number 16 in [16, 17) is still being tracked, despite the remove operation)
Note:微信
[left, right)
denotes all real numbers left <= x < right
.0 < left < right < 10^9
in all calls to addRange, queryRange, removeRange
.addRange
in a single test case is at most 1000
.queryRange
in a single test case is at most 5000
.removeRange
in a single test case is at most 1000
.Range 模块是跟踪数字范围的模块。你的任务是以一种有效的方式设计和实现如下接口。app
addRange(int left, int right)
添加半开区间 [left, right)
,跟踪该区间中的每一个实数。添加与当前跟踪的数字部分重叠的区间时,应当添加在区间 [left, right)
中还没有跟踪的任何数字到该区间中。queryRange(int left, int right)
只有在当前正在跟踪区间 [left, right)
中的每个实数时,才返回 true。removeRange(int left, int right)
中止跟踪区间 [left, right)
中当前正在跟踪的每一个实数。示例:测试
addRange(10, 20): null removeRange(14, 16): null queryRange(10, 14): true (区间 [10, 14) 中的每一个数都正在被跟踪) queryRange(13, 15): false (未跟踪区间 [13, 15) 中像 14, 14.03, 14.17 这样的数字) queryRange(16, 17): true (尽管执行了删除操做,区间 [16, 17) 中的数字 16 仍然会被跟踪)
提示:spa
[left, right)
表示全部知足 left <= x < right
的实数。addRange, queryRange, removeRange
的全部调用中 0 < left < right < 10^9
。addRange
的调用总数不超过 1000
次。queryRange
的调用总数不超过 5000
次。removeRange
的调用总数不超过 1000
次。1 class RangeModule { 2 var v:[(Int,Int)] 3 4 init() { 5 v = [(Int,Int)]() 6 } 7 8 func addRange(_ left: Int, _ right: Int) { 9 var left = left 10 var right = right 11 var res:[(Int,Int)] = [(Int,Int)]() 12 var n:Int = v.count 13 var cur:Int = 0 14 for i in 0..<n 15 { 16 if v[i].1 < left 17 { 18 res.append(v[i]) 19 cur += 1 20 } 21 else if v[i].0 > right 22 { 23 res.append(v[i]) 24 } 25 else 26 { 27 left = min(left, v[i].0) 28 right = max(right, v[i].1) 29 } 30 } 31 res.insert((left, right),at:cur) 32 v = res 33 } 34 35 func queryRange(_ left: Int, _ right: Int) -> Bool { 36 for a in v 37 { 38 if a.0 <= left && a.1 >= right 39 { 40 return true 41 } 42 } 43 return false 44 } 45 46 func removeRange(_ left: Int, _ right: Int) { 47 var res:[(Int,Int)] = [(Int,Int)]() 48 var t:[(Int,Int)] = [(Int,Int)]() 49 var n:Int = v.count 50 var cur:Int = 0 51 for i in 0..<n 52 { 53 if v[i].1 <= left 54 { 55 res.append(v[i]) 56 cur += 1 57 } 58 else if v[i].0 >= right 59 { 60 res.append(v[i]) 61 } 62 else 63 { 64 if v[i].0 < left 65 { 66 t.append((v[i].0, left)) 67 } 68 if v[i].1 > right 69 { 70 t.append((right, v[i].1)) 71 } 72 } 73 } 74 res += t 75 v = res 76 } 77 } 78 79 /** 80 * Your RangeModule object will be instantiated and called as such: 81 * let obj = RangeModule() 82 * obj.addRange(left, right) 83 * let ret_2: Bool = obj.queryRange(left, right) 84 * obj.removeRange(left, right) 85 */