|
43 | 43 |
|
44 | 44 | <p><meta charset="UTF-8" />注意:本题与主站 220 题相同: <a href="https://leetcode-cn.com/problems/contains-duplicate-iii/">https://leetcode-cn.com/problems/contains-duplicate-iii/</a></p>
|
45 | 45 |
|
46 |
| - |
47 | 46 | ## 解法
|
48 | 47 |
|
49 | 48 | <!-- 这里可写通用的实现逻辑 -->
|
50 | 49 |
|
| 50 | +“滑动窗口 + 有序集合”实现。 |
| 51 | + |
51 | 52 | <!-- tabs:start -->
|
52 | 53 |
|
53 | 54 | ### **Python3**
|
54 | 55 |
|
55 | 56 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
56 | 57 |
|
57 | 58 | ```python
|
58 |
| - |
| 59 | +from sortedcontainers import SortedSet |
| 60 | + |
| 61 | + |
| 62 | +class Solution: |
| 63 | + def containsNearbyAlmostDuplicate(self, nums: List[int], k: int, t: int) -> bool: |
| 64 | + s = SortedSet() |
| 65 | + for i, num in enumerate(nums): |
| 66 | + idx = s.bisect_left(num - t) |
| 67 | + if 0 <= idx < len(s) and s[idx] <= num + t: |
| 68 | + return True |
| 69 | + s.add(num) |
| 70 | + if i >= k: |
| 71 | + s.remove(nums[i - k]) |
| 72 | + return False |
59 | 73 | ```
|
60 | 74 |
|
61 | 75 | ### **Java**
|
62 | 76 |
|
63 | 77 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
64 | 78 |
|
65 | 79 | ```java
|
| 80 | +class Solution { |
| 81 | + public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) { |
| 82 | + TreeSet<Long> ts = new TreeSet<>(); |
| 83 | + for (int i = 0; i < nums.length; ++i) { |
| 84 | + Long x = ts.ceiling((long) nums[i] - (long) t); |
| 85 | + if (x != null && x <= (long) nums[i] + (long) t) { |
| 86 | + return true; |
| 87 | + } |
| 88 | + ts.add((long) nums[i]); |
| 89 | + if (i >= k) { |
| 90 | + ts.remove((long) nums[i - k]); |
| 91 | + } |
| 92 | + } |
| 93 | + return false; |
| 94 | + } |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +### **C++** |
| 99 | + |
| 100 | +```cpp |
| 101 | +class Solution { |
| 102 | +public: |
| 103 | + bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) { |
| 104 | + set<long> s; |
| 105 | + for (int i = 0; i < nums.size(); ++i) |
| 106 | + { |
| 107 | + auto it = s.lower_bound((long) nums[i] - t); |
| 108 | + if (it != s.end() && *it <= (long) nums[i] + t) return true; |
| 109 | + s.insert((long) nums[i]); |
| 110 | + if (i >= k) s.erase((long) nums[i - k]); |
| 111 | + } |
| 112 | + return false; |
| 113 | + } |
| 114 | +}; |
| 115 | +``` |
66 | 116 |
|
| 117 | +### **Go** |
| 118 | +
|
| 119 | +```go |
| 120 | +import "math/rand" |
| 121 | +
|
| 122 | +type node struct { |
| 123 | + ch [2]*node |
| 124 | + priority int |
| 125 | + val int |
| 126 | +} |
| 127 | +
|
| 128 | +func (o *node) cmp(b int) int { |
| 129 | + switch { |
| 130 | + case b < o.val: |
| 131 | + return 0 |
| 132 | + case b > o.val: |
| 133 | + return 1 |
| 134 | + default: |
| 135 | + return -1 |
| 136 | + } |
| 137 | +} |
| 138 | +
|
| 139 | +func (o *node) rotate(d int) *node { |
| 140 | + x := o.ch[d^1] |
| 141 | + o.ch[d^1] = x.ch[d] |
| 142 | + x.ch[d] = o |
| 143 | + return x |
| 144 | +} |
| 145 | +
|
| 146 | +type treap struct { |
| 147 | + root *node |
| 148 | +} |
| 149 | +
|
| 150 | +func (t *treap) _put(o *node, val int) *node { |
| 151 | + if o == nil { |
| 152 | + return &node{priority: rand.Int(), val: val} |
| 153 | + } |
| 154 | + d := o.cmp(val) |
| 155 | + o.ch[d] = t._put(o.ch[d], val) |
| 156 | + if o.ch[d].priority > o.priority { |
| 157 | + o = o.rotate(d ^ 1) |
| 158 | + } |
| 159 | + return o |
| 160 | +} |
| 161 | +
|
| 162 | +func (t *treap) put(val int) { |
| 163 | + t.root = t._put(t.root, val) |
| 164 | +} |
| 165 | +
|
| 166 | +func (t *treap) _delete(o *node, val int) *node { |
| 167 | + if d := o.cmp(val); d >= 0 { |
| 168 | + o.ch[d] = t._delete(o.ch[d], val) |
| 169 | + return o |
| 170 | + } |
| 171 | + if o.ch[1] == nil { |
| 172 | + return o.ch[0] |
| 173 | + } |
| 174 | + if o.ch[0] == nil { |
| 175 | + return o.ch[1] |
| 176 | + } |
| 177 | + d := 0 |
| 178 | + if o.ch[0].priority > o.ch[1].priority { |
| 179 | + d = 1 |
| 180 | + } |
| 181 | + o = o.rotate(d) |
| 182 | + o.ch[d] = t._delete(o.ch[d], val) |
| 183 | + return o |
| 184 | +} |
| 185 | +
|
| 186 | +func (t *treap) delete(val int) { |
| 187 | + t.root = t._delete(t.root, val) |
| 188 | +} |
| 189 | +
|
| 190 | +func (t *treap) lowerBound(val int) (lb *node) { |
| 191 | + for o := t.root; o != nil; { |
| 192 | + switch c := o.cmp(val); { |
| 193 | + case c == 0: |
| 194 | + lb = o |
| 195 | + o = o.ch[0] |
| 196 | + case c > 0: |
| 197 | + o = o.ch[1] |
| 198 | + default: |
| 199 | + return o |
| 200 | + } |
| 201 | + } |
| 202 | + return |
| 203 | +} |
| 204 | +
|
| 205 | +func containsNearbyAlmostDuplicate(nums []int, k, t int) bool { |
| 206 | + s := &treap{} |
| 207 | + for i, num := range nums { |
| 208 | + if lb := s.lowerBound(num - t); lb != nil && lb.val <= num+t { |
| 209 | + return true |
| 210 | + } |
| 211 | + s.put(num) |
| 212 | + if i >= k { |
| 213 | + s.delete(nums[i-k]) |
| 214 | + } |
| 215 | + } |
| 216 | + return false |
| 217 | +} |
67 | 218 | ```
|
68 | 219 |
|
69 | 220 | ### **...**
|
|
0 commit comments