Skip to content

Commit 48a2c6a

Browse files
committed
update 217&220 notes
1 parent 90cd29e commit 48a2c6a

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

solutions/217.Contains_Duplicate/readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
## 217. Contains Duplicate
22

33
### **链接**
4-
题目:https://leetcode.com/problems/contains-duplicate
4+
题目:https://leetcode.com/problems/contains-duplicate
55
代码(github):https://github.com/illuz/leetcode
66

77
### **题意**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
## 220. Contains Duplicate III
2+
3+
### **链接**
4+
题目:https://leetcode.com/problems/contains-duplicate-iii/
5+
代码(github):https://github.com/illuz/leetcode
6+
7+
### **题意**
8+
9+
问一个数组中有没有重复的数,且这两个数的下标差距不超过 k,而且值差距不超过 t。
10+
11+
系列文章:
12+
13+
- [217. Contains Duplicate](https://leetcode.com/problems/contains-duplicate)
14+
- [219. Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/)
15+
- [220. Contains Duplicate III](https://leetcode.com/problems/contains-duplicate-iii/)
16+
17+
### **分析**
18+
19+
思路是维护一个大小不超过 k 的窗口,每加一个数前判断这个窗口中有没有差大于 t 的数。(nlogn)
20+
主要考虑的是数据结构。
21+
22+
1. (C++)用 set 和 multiset 都可以做,里面已经有封装 lower_bound 和 upper_bound 了,很方便。
23+
2. (C++)当然你可以手写 BST,比较麻烦。
24+
3. (Java)用 TreeSet 的 subSet 简直酸爽!
25+
26+
**O(n) 算法**
27+
Discuss 大神写的,实在太神了!
28+
就是把数都放到一个个桶里面,再去判断。
29+
链接:https://leetcode.com/discuss/38206/ac-o-n-solution-in-java-using-buckets-with-explanation
30+
31+
PS:这题的样例有并不全面,test 只有有序的数组,所以 `[1,9,3,16,21,2], 2, 2` 返回了 false 的程序也对了。

0 commit comments

Comments
 (0)