Skip to content

Commit 840ea4e

Browse files
committed
Added Contains Duplicate I II II
1 parent d84f3c5 commit 840ea4e

8 files changed

Lines changed: 197 additions & 0 deletions

‎java/SUMMARY.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
* [Candy](linear-list/array/candy.md)
2525
* [Majority Element](linear-list/array/majority-element.md)
2626
* [Rotate Array](linear-list/array/rotate-array.md)
27+
* [Contains Duplicate](linear-list/array/contains-duplicate.md)
28+
* [Contains Duplicate II](linear-list/array/contains-duplicate-ii.md)
29+
* [Contains Duplicate III](linear-list/array/contains-duplicate-iii.md)
2730
* [单链表](linear-list/linked-list/README.md)
2831
* [Add Two Numbers](linear-list/linked-list/add-two-numbers.md)
2932
* [Reverse Linked List II](linear-list/linked-list/reverse-linked-list-ii.md)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Contains Duplicate
2+
// Time Complexity: O(n), Space Complexity: O(n)
3+
public class Solution {
4+
public boolean containsDuplicate(int[] nums) {
5+
final Set<Integer> existed = new HashSet<>();
6+
for (int x : nums) {
7+
if (existed.contains(x)) {
8+
return true;
9+
} else {
10+
existed.add(x);
11+
}
12+
}
13+
return false;
14+
}
15+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Contains Duplicate
2+
// Time Complexity: O(nlogn), Space Complexity: O(1)
3+
public class Solution {
4+
public boolean containsDuplicate(int[] nums) {
5+
Arrays.sort(nums);
6+
7+
for (int i = 1; i < nums.length; ++i) {
8+
if (nums[i-1] == nums[i]) return true;
9+
}
10+
return false;
11+
}
12+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Contains Duplicate II
2+
// Time Complexity: O(n), Space Complexity: O(n)
3+
public class Solution {
4+
public boolean containsNearbyDuplicate(int[] nums, int k) {
5+
final Map<Integer, Integer> map = new HashMap<>();
6+
int min = Integer.MAX_VALUE;
7+
8+
for(int i = 0; i < nums.length; i++){
9+
if(map.containsKey(nums[i])){
10+
final int preIndex = map.get(nums[i]);
11+
final int gap = i - preIndex;
12+
min = Math.min(min, gap);
13+
}
14+
map.put(nums[i], i);
15+
}
16+
return min <= k;
17+
}
18+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Contains Duplicate III
2+
// Time Complexity: O(nlogk), Space Complexity: O(k)
3+
public class Solution {
4+
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
5+
if (k < 1 || t < 0) return false;
6+
7+
final TreeSet<Integer> set = new TreeSet<>();
8+
for (int i = 0; i < nums.length; i++) {
9+
final int x = nums[i];
10+
final Integer floor = set.floor(x);
11+
final Integer ceiling = set.ceiling(x);
12+
13+
if ((floor != null && x <= floor + t)
14+
|| (ceiling != null && x >= ceiling -t))
15+
return true;
16+
17+
set.add(x);
18+
if (i >= k) set.remove(nums[i - k]);
19+
}
20+
21+
return false;
22+
}
23+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
## Contains Duplicate II
2+
3+
4+
### 描述
5+
6+
Given an array of integers and an integer `k`, find out whether there are two distinct indices `i` and `j` in the array such that `nums[i] = nums[j]` and the difference between `i` and `j` is at most `k`.
7+
8+
9+
### 分析
10+
11+
维护一个HashMap, key为整数,value为下标,将数组中的元素不断添加进这个HashMap, 碰到重复时,计算二者的下标距离,如果距离小于或等于k,则返回true, 如果直到数组扫描完,距离都大于k,则返回false。
12+
13+
14+
### 代码
15+
16+
{% if book.java %}
17+
```java
18+
// Contains Duplicate II
19+
// Time Complexity: O(n), Space Complexity: O(n)
20+
public class Solution {
21+
public boolean containsNearbyDuplicate(int[] nums, int k) {
22+
final Map<Integer, Integer> map = new HashMap<>();
23+
int min = Integer.MAX_VALUE;
24+
25+
for(int i = 0; i < nums.length; i++){
26+
if(map.containsKey(nums[i])){
27+
final int preIndex = map.get(nums[i]);
28+
final int gap = i - preIndex;
29+
min = Math.min(min, gap);
30+
}
31+
map.put(nums[i], i);
32+
}
33+
return min <= k;
34+
}
35+
}
36+
```
37+
{% endif %}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
## Contains Duplicate III
2+
3+
4+
### 描述
5+
6+
Given an array of integers, find out whether there are two distinct indices `i` and `j` in the array such that the difference between `nums[i]` and `nums[j]` is at most `t` and the difference between `i` and `j` is at most `k`.
7+
8+
### 分析
9+
10+
这一题比 "Contains Duplicate II" 有多了个条件,难度陡然就增大了。
11+
12+
对于数组内的一个整数,如果能方便的求出大于它的最小整数和小于它的最大整数,那么我们就可以判断差值是否大于`t`。能方便的求出最大下限和最小上限,最先想到的数据结构是二叉搜索树BST,因为左孩子就是最大下限,右孩子就是最小上限。
13+
14+
可以用一个大小为k的滑动窗口,将窗口内的元素组织成一个BST,每次向前滑动一步,添加一个新元素,同时删除一个最老的元素,如此不断向前滑动,不断更新BST。如果BST内部有两个元素差值大于`t`,则返回true,如果直到扫描完数组,BST里都没有出现差值大于k的两个数,则返回false。
15+
16+
对于BST数据结构,可以用现成的,C++里是`multiset`,Java里是`TreeSet`。
17+
18+
19+
### 代码 滑动窗口+BST
20+
21+
{% if book.java %}
22+
```java
23+
// Contains Duplicate III
24+
// Time Complexity: O(nlogk), Space Complexity: O(k)
25+
public class Solution {
26+
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
27+
if (k < 1 || t < 0) return false;
28+
29+
final TreeSet<Integer> set = new TreeSet<>();
30+
for (int i = 0; i < nums.length; i++) {
31+
final int x = nums[i];
32+
final Integer floor = set.floor(x);
33+
final Integer ceiling = set.ceiling(x);
34+
35+
if ((floor != null && x <= floor + t)
36+
|| (ceiling != null && x >= ceiling -t))
37+
return true;
38+
39+
set.add(x);
40+
if (i >= k) set.remove(nums[i - k]);
41+
}
42+
43+
return false;
44+
}
45+
}
46+
```
47+
{% endif %}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
## Contains Duplicate
2+
3+
4+
### 描述
5+
6+
Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.
7+
8+
### 分析
9+
10+
方法1, 用一个 HashSet, 不断往里面塞元素,如果发现有重复,说明存在重复。时间复杂度`O(n)`,空间复杂度`O(n)`。
11+
12+
方法2, 排序,然后从头到尾扫描,如果发现相邻两个元素相等,则存在重复。时间复杂度`O(nlogn)`,空间复杂度`O(1)`。
13+
14+
15+
### 解法1 哈希表
16+
17+
{% if book.java %}
18+
```java
19+
// Contains Duplicate
20+
// Time Complexity: O(n), Space Complexity: O(n)
21+
public class Solution {
22+
public boolean containsDuplicate(int[] nums) {
23+
final Set<Integer> existed = new HashSet<>();
24+
for (int x : nums) {
25+
if (existed.contains(x)) {
26+
return true;
27+
} else {
28+
existed.add(x);
29+
}
30+
}
31+
return false;
32+
}
33+
}
34+
```
35+
{% endif %}
36+
37+
38+
### 解法2 排序
39+
40+
{% if book.java %}
41+
{% codesnippet "./code/contains-duplicate-2."+book.suffix, language=book.suffix %}{% endcodesnippet %}
42+
{% endif %}

0 commit comments

Comments
 (0)