Skip to content

Commit 702d3e0

Browse files
committed
feat: add solutions to lc/lcof2 problems: Contains Duplicate III
lc.No.0220 & lcof2 No.057. Contains Duplicate III
1 parent 105093d commit 702d3e0

File tree

11 files changed

+739
-31
lines changed

11 files changed

+739
-31
lines changed

lcof2/剑指 Offer II 057. 值和下标之差都在给定的范围内/README.md

+153-2
Original file line numberDiff line numberDiff line change
@@ -43,27 +43,178 @@
4343

4444
<p><meta charset="UTF-8" />注意:本题与主站 220&nbsp;题相同:&nbsp;<a href="https://leetcode-cn.com/problems/contains-duplicate-iii/">https://leetcode-cn.com/problems/contains-duplicate-iii/</a></p>
4545

46-
4746
## 解法
4847

4948
<!-- 这里可写通用的实现逻辑 -->
5049

50+
“滑动窗口 + 有序集合”实现。
51+
5152
<!-- tabs:start -->
5253

5354
### **Python3**
5455

5556
<!-- 这里可写当前语言的特殊实现逻辑 -->
5657

5758
```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
5973
```
6074

6175
### **Java**
6276

6377
<!-- 这里可写当前语言的特殊实现逻辑 -->
6478

6579
```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+
```
66116
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+
}
67218
```
68219

69220
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
4+
set<long> s;
5+
for (int i = 0; i < nums.size(); ++i)
6+
{
7+
auto it = s.lower_bound((long) nums[i] - t);
8+
if (it != s.end() && *it <= (long) nums[i] + t) return true;
9+
s.insert((long) nums[i]);
10+
if (i >= k) s.erase((long) nums[i - k]);
11+
}
12+
return false;
13+
}
14+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import "math/rand"
2+
3+
type node struct {
4+
ch [2]*node
5+
priority int
6+
val int
7+
}
8+
9+
func (o *node) cmp(b int) int {
10+
switch {
11+
case b < o.val:
12+
return 0
13+
case b > o.val:
14+
return 1
15+
default:
16+
return -1
17+
}
18+
}
19+
20+
func (o *node) rotate(d int) *node {
21+
x := o.ch[d^1]
22+
o.ch[d^1] = x.ch[d]
23+
x.ch[d] = o
24+
return x
25+
}
26+
27+
type treap struct {
28+
root *node
29+
}
30+
31+
func (t *treap) _put(o *node, val int) *node {
32+
if o == nil {
33+
return &node{priority: rand.Int(), val: val}
34+
}
35+
d := o.cmp(val)
36+
o.ch[d] = t._put(o.ch[d], val)
37+
if o.ch[d].priority > o.priority {
38+
o = o.rotate(d ^ 1)
39+
}
40+
return o
41+
}
42+
43+
func (t *treap) put(val int) {
44+
t.root = t._put(t.root, val)
45+
}
46+
47+
func (t *treap) _delete(o *node, val int) *node {
48+
if d := o.cmp(val); d >= 0 {
49+
o.ch[d] = t._delete(o.ch[d], val)
50+
return o
51+
}
52+
if o.ch[1] == nil {
53+
return o.ch[0]
54+
}
55+
if o.ch[0] == nil {
56+
return o.ch[1]
57+
}
58+
d := 0
59+
if o.ch[0].priority > o.ch[1].priority {
60+
d = 1
61+
}
62+
o = o.rotate(d)
63+
o.ch[d] = t._delete(o.ch[d], val)
64+
return o
65+
}
66+
67+
func (t *treap) delete(val int) {
68+
t.root = t._delete(t.root, val)
69+
}
70+
71+
func (t *treap) lowerBound(val int) (lb *node) {
72+
for o := t.root; o != nil; {
73+
switch c := o.cmp(val); {
74+
case c == 0:
75+
lb = o
76+
o = o.ch[0]
77+
case c > 0:
78+
o = o.ch[1]
79+
default:
80+
return o
81+
}
82+
}
83+
return
84+
}
85+
86+
func containsNearbyAlmostDuplicate(nums []int, k, t int) bool {
87+
s := &treap{}
88+
for i, num := range nums {
89+
if lb := s.lowerBound(num - t); lb != nil && lb.val <= num+t {
90+
return true
91+
}
92+
s.put(num)
93+
if i >= k {
94+
s.delete(nums[i-k])
95+
}
96+
}
97+
return false
98+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
3+
TreeSet<Long> ts = new TreeSet<>();
4+
for (int i = 0; i < nums.length; ++i) {
5+
Long x = ts.ceiling((long) nums[i] - (long) t);
6+
if (x != null && x <= (long) nums[i] + (long) t) {
7+
return true;
8+
}
9+
ts.add((long) nums[i]);
10+
if (i >= k) {
11+
ts.remove((long) nums[i - k]);
12+
}
13+
}
14+
return false;
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from sortedcontainers import SortedSet
2+
3+
4+
class Solution:
5+
def containsNearbyAlmostDuplicate(self, nums: List[int], k: int, t: int) -> bool:
6+
s = SortedSet()
7+
for i, num in enumerate(nums):
8+
idx = s.bisect_left(num - t)
9+
if 0 <= idx < len(s) and s[idx] <= num + t:
10+
return True
11+
s.add(num)
12+
if i >= k:
13+
s.remove(nums[i - k])
14+
return False

0 commit comments

Comments
 (0)