Skip to content

Commit ed7bd4b

Browse files
committed
feat: add solutions to lc problem: No.0219
No.0219.Contains Duplicate II
1 parent e406010 commit ed7bd4b

File tree

8 files changed

+86
-85
lines changed

8 files changed

+86
-85
lines changed

solution/0200-0299/0219.Contains Duplicate II/README.md

+26-29
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646

4747
**方法一:哈希表**
4848

49-
我们用哈希表存放最近遍历到的数以及对应的下标
49+
我们用哈希表 $d$ 存放最近遍历到的数以及对应的下标
5050

5151
遍历数组 `nums`,对于当前遍历到的元素 $nums[i]$,如果在哈希表中存在,并且下标与当前元素的下标之差不超过 $k$,则返回 `true`,否则将当前元素加入哈希表中。
5252

@@ -63,11 +63,11 @@
6363
```python
6464
class Solution:
6565
def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
66-
mp = {}
67-
for i, v in enumerate(nums):
68-
if v in mp and i - mp[v] <= k:
66+
d = {}
67+
for i, x in enumerate(nums):
68+
if x in d and i - d[x] <= k:
6969
return True
70-
mp[v] = i
70+
d[x] = i
7171
return False
7272
```
7373

@@ -78,12 +78,12 @@ class Solution:
7878
```java
7979
class Solution {
8080
public boolean containsNearbyDuplicate(int[] nums, int k) {
81-
Map<Integer, Integer> mp = new HashMap<>();
81+
Map<Integer, Integer> d = new HashMap<>();
8282
for (int i = 0; i < nums.length; ++i) {
83-
if (mp.containsKey(nums[i]) && i - mp.get(nums[i]) <= k) {
83+
if (i - d.getOrDefault(nums[i], -1000000) <= k) {
8484
return true;
8585
}
86-
mp.put(nums[i], i);
86+
d.put(nums[i], i);
8787
}
8888
return false;
8989
}
@@ -96,10 +96,12 @@ class Solution {
9696
class Solution {
9797
public:
9898
bool containsNearbyDuplicate(vector<int>& nums, int k) {
99-
unordered_map<int, int> mp;
99+
unordered_map<int, int> d;
100100
for (int i = 0; i < nums.size(); ++i) {
101-
if (mp.count(nums[i]) && i - mp[nums[i]] <= k) return true;
102-
mp[nums[i]] = i;
101+
if (d.count(nums[i]) && i - d[nums[i]] <= k) {
102+
return true;
103+
}
104+
d[nums[i]] = i;
103105
}
104106
return false;
105107
}
@@ -110,14 +112,12 @@ public:
110112
111113
```go
112114
func containsNearbyDuplicate(nums []int, k int) bool {
113-
mp := make(map[int]int)
114-
for i, v := range nums {
115-
if j, ok := mp[v]; ok {
116-
if i-j <= k {
117-
return true
118-
}
115+
d := map[int]int{}
116+
for i, x := range nums {
117+
if j, ok := d[x]; ok && i-j <= k {
118+
return true
119119
}
120-
mp[v] = i
120+
d[x] = i
121121
}
122122
return false
123123
}
@@ -128,14 +128,12 @@ func containsNearbyDuplicate(nums []int, k int) bool {
128128
```cs
129129
public class Solution {
130130
public bool ContainsNearbyDuplicate(int[] nums, int k) {
131-
var mp = new Dictionary<int, int>();
132-
for (int i = 0; i < nums.Length; ++i)
133-
{
134-
if (mp.ContainsKey(nums[i]) && i - mp[nums[i]] <= k)
135-
{
131+
var d = new Dictionary<int, int>();
132+
for (int i = 0; i < nums.Length; ++i) {
133+
if (d.ContainsKey(nums[i]) && i - d[nums[i]] <= k) {
136134
return true;
137135
}
138-
mp[nums[i]] = i;
136+
d[nums[i]] = i;
139137
}
140138
return false;
141139
}
@@ -146,13 +144,12 @@ public class Solution {
146144

147145
```ts
148146
function containsNearbyDuplicate(nums: number[], k: number): boolean {
149-
const map = new Map();
150-
for (let i = 0; i < nums.length; i++) {
151-
const t = nums[i];
152-
if (map.has(t) && i - map.get(t) <= k) {
147+
const d: Map<number, number> = new Map();
148+
for (let i = 0; i < nums.length; ++i) {
149+
if (d.has(nums[i]) && i - d.get(nums[i])! <= k) {
153150
return true;
154151
}
155-
map.set(t, i);
152+
d.set(nums[i], i);
156153
}
157154
return false;
158155
}

solution/0200-0299/0219.Contains Duplicate II/README_EN.md

+35-28
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,28 @@
3939

4040
## Solutions
4141

42+
**Approach 1: Hash Table**
43+
44+
We use a hash table $d$ to store the nearest index of the number it has visited.
45+
46+
We traverse the array `nums`. For the current element $nums[i]$, if it exists in the hash table, and the difference between its index and the current index is no larger than $k$, then return `true`. Otherwise, we add the current element into the hash table.
47+
48+
After the traversal, return `false`.
49+
50+
The time complexity is $O(n)$ and the space complexity is $O(n)$. Here $n$ is the length of array `nums`.
51+
4252
<!-- tabs:start -->
4353

4454
### **Python3**
4555

4656
```python
4757
class Solution:
4858
def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
49-
mp = {}
50-
for i, v in enumerate(nums):
51-
if v in mp and i - mp[v] <= k:
59+
d = {}
60+
for i, x in enumerate(nums):
61+
if x in d and i - d[x] <= k:
5262
return True
53-
mp[v] = i
63+
d[x] = i
5464
return False
5565
```
5666

@@ -59,12 +69,12 @@ class Solution:
5969
```java
6070
class Solution {
6171
public boolean containsNearbyDuplicate(int[] nums, int k) {
62-
Map<Integer, Integer> mp = new HashMap<>();
72+
Map<Integer, Integer> d = new HashMap<>();
6373
for (int i = 0; i < nums.length; ++i) {
64-
if (mp.containsKey(nums[i]) && i - mp.get(nums[i]) <= k) {
74+
if (i - d.getOrDefault(nums[i], -1000000) <= k) {
6575
return true;
6676
}
67-
mp.put(nums[i], i);
77+
d.put(nums[i], i);
6878
}
6979
return false;
7080
}
@@ -77,10 +87,12 @@ class Solution {
7787
class Solution {
7888
public:
7989
bool containsNearbyDuplicate(vector<int>& nums, int k) {
80-
unordered_map<int, int> mp;
90+
unordered_map<int, int> d;
8191
for (int i = 0; i < nums.size(); ++i) {
82-
if (mp.count(nums[i]) && i - mp[nums[i]] <= k) return true;
83-
mp[nums[i]] = i;
92+
if (d.count(nums[i]) && i - d[nums[i]] <= k) {
93+
return true;
94+
}
95+
d[nums[i]] = i;
8496
}
8597
return false;
8698
}
@@ -91,14 +103,12 @@ public:
91103
92104
```go
93105
func containsNearbyDuplicate(nums []int, k int) bool {
94-
mp := make(map[int]int)
95-
for i, v := range nums {
96-
if j, ok := mp[v]; ok {
97-
if i-j <= k {
98-
return true
99-
}
106+
d := map[int]int{}
107+
for i, x := range nums {
108+
if j, ok := d[x]; ok && i-j <= k {
109+
return true
100110
}
101-
mp[v] = i
111+
d[x] = i
102112
}
103113
return false
104114
}
@@ -109,14 +119,12 @@ func containsNearbyDuplicate(nums []int, k int) bool {
109119
```cs
110120
public class Solution {
111121
public bool ContainsNearbyDuplicate(int[] nums, int k) {
112-
var mp = new Dictionary<int, int>();
113-
for (int i = 0; i < nums.Length; ++i)
114-
{
115-
if (mp.ContainsKey(nums[i]) && i - mp[nums[i]] <= k)
116-
{
122+
var d = new Dictionary<int, int>();
123+
for (int i = 0; i < nums.Length; ++i) {
124+
if (d.ContainsKey(nums[i]) && i - d[nums[i]] <= k) {
117125
return true;
118126
}
119-
mp[nums[i]] = i;
127+
d[nums[i]] = i;
120128
}
121129
return false;
122130
}
@@ -127,13 +135,12 @@ public class Solution {
127135

128136
```ts
129137
function containsNearbyDuplicate(nums: number[], k: number): boolean {
130-
const map = new Map();
131-
for (let i = 0; i < nums.length; i++) {
132-
const t = nums[i];
133-
if (map.has(t) && i - map.get(t) <= k) {
138+
const d: Map<number, number> = new Map();
139+
for (let i = 0; i < nums.length; ++i) {
140+
if (d.has(nums[i]) && i - d.get(nums[i])! <= k) {
134141
return true;
135142
}
136-
map.set(t, i);
143+
d.set(nums[i], i);
137144
}
138145
return false;
139146
}

solution/0200-0299/0219.Contains Duplicate II/Solution.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
class Solution {
22
public:
33
bool containsNearbyDuplicate(vector<int>& nums, int k) {
4-
unordered_map<int, int> mp;
4+
unordered_map<int, int> d;
55
for (int i = 0; i < nums.size(); ++i) {
6-
if (mp.count(nums[i]) && i - mp[nums[i]] <= k) return true;
7-
mp[nums[i]] = i;
6+
if (d.count(nums[i]) && i - d[nums[i]] <= k) {
7+
return true;
8+
}
9+
d[nums[i]] = i;
810
}
911
return false;
1012
}

solution/0200-0299/0219.Contains Duplicate II/Solution.cs

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
public class Solution {
22
public bool ContainsNearbyDuplicate(int[] nums, int k) {
3-
var mp = new Dictionary<int, int>();
4-
for (int i = 0; i < nums.Length; ++i)
5-
{
6-
if (mp.ContainsKey(nums[i]) && i - mp[nums[i]] <= k)
7-
{
3+
var d = new Dictionary<int, int>();
4+
for (int i = 0; i < nums.Length; ++i) {
5+
if (d.ContainsKey(nums[i]) && i - d[nums[i]] <= k) {
86
return true;
97
}
10-
mp[nums[i]] = i;
8+
d[nums[i]] = i;
119
}
1210
return false;
1311
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
func containsNearbyDuplicate(nums []int, k int) bool {
2-
mp := make(map[int]int)
3-
for i, v := range nums {
4-
if j, ok := mp[v]; ok {
5-
if i-j <= k {
6-
return true
7-
}
2+
d := map[int]int{}
3+
for i, x := range nums {
4+
if j, ok := d[x]; ok && i-j <= k {
5+
return true
86
}
9-
mp[v] = i
7+
d[x] = i
108
}
119
return false
1210
}

solution/0200-0299/0219.Contains Duplicate II/Solution.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
class Solution {
22
public boolean containsNearbyDuplicate(int[] nums, int k) {
3-
Map<Integer, Integer> mp = new HashMap<>();
3+
Map<Integer, Integer> d = new HashMap<>();
44
for (int i = 0; i < nums.length; ++i) {
5-
if (mp.containsKey(nums[i]) && i - mp.get(nums[i]) <= k) {
5+
if (i - d.getOrDefault(nums[i], -1000000) <= k) {
66
return true;
77
}
8-
mp.put(nums[i], i);
8+
d.put(nums[i], i);
99
}
1010
return false;
1111
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
class Solution:
22
def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
3-
mp = {}
4-
for i, v in enumerate(nums):
5-
if v in mp and i - mp[v] <= k:
3+
d = {}
4+
for i, x in enumerate(nums):
5+
if x in d and i - d[x] <= k:
66
return True
7-
mp[v] = i
7+
d[x] = i
88
return False
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
function containsNearbyDuplicate(nums: number[], k: number): boolean {
2-
const map = new Map();
3-
for (let i = 0; i < nums.length; i++) {
4-
const t = nums[i];
5-
if (map.has(t) && i - map.get(t) <= k) {
2+
const d: Map<number, number> = new Map();
3+
for (let i = 0; i < nums.length; ++i) {
4+
if (d.has(nums[i]) && i - d.get(nums[i])! <= k) {
65
return true;
76
}
8-
map.set(t, i);
7+
d.set(nums[i], i);
98
}
109
return false;
1110
}

0 commit comments

Comments
 (0)