★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(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 */