Skip to content

Commit fe7b259

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

File tree

7 files changed

+160
-62
lines changed

7 files changed

+160
-62
lines changed

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

+61-11
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@
3838
```python
3939
class Solution:
4040
def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
41-
helper = {}
41+
mp = {}
4242
for i, v in enumerate(nums):
43-
if v in helper and i - helper[v] <= k:
43+
if v in mp and i - mp[v] <= k:
4444
return True
45-
helper[v] = i
45+
mp[v] = i
4646
return False
4747
```
4848

@@ -53,15 +53,65 @@ class Solution:
5353
```java
5454
class Solution {
5555
public boolean containsNearbyDuplicate(int[] nums, int k) {
56-
Map<Integer, Integer> helper = new HashMap<>();
57-
for (int i = 0, n = nums.length; i < n; ++i) {
58-
if (helper.containsKey(nums[i])) {
59-
int j = helper.get(nums[i]);
60-
if (i - j <= k) {
61-
return true;
62-
}
56+
Map<Integer, Integer> mp = new HashMap<>();
57+
for (int i = 0; i < nums.length; ++i) {
58+
if (mp.containsKey(nums[i]) && i - mp.get(nums[i]) <= k) {
59+
return true;
6360
}
64-
helper.put(nums[i], i);
61+
mp.put(nums[i], i);
62+
}
63+
return false;
64+
}
65+
}
66+
```
67+
68+
### **C++**
69+
70+
```cpp
71+
class Solution {
72+
public:
73+
bool containsNearbyDuplicate(vector<int>& nums, int k) {
74+
unordered_map<int, int> mp;
75+
for (int i = 0; i < nums.size(); ++i)
76+
{
77+
if (mp.count(nums[i]) && i - mp[nums[i]] <= k) return true;
78+
mp[nums[i]] = i;
79+
}
80+
return false;
81+
}
82+
};
83+
```
84+
85+
### **Go**
86+
87+
```go
88+
func containsNearbyDuplicate(nums []int, k int) bool {
89+
mp := make(map[int]int)
90+
for i, v := range nums {
91+
if j, ok := mp[v]; ok {
92+
if i-j <= k {
93+
return true
94+
}
95+
}
96+
mp[v] = i
97+
}
98+
return false
99+
}
100+
```
101+
102+
### **C#**
103+
104+
```cs
105+
public class Solution {
106+
public bool ContainsNearbyDuplicate(int[] nums, int k) {
107+
var mp = new Dictionary<int, int>();
108+
for (int i = 0; i < nums.Length; ++i)
109+
{
110+
if (mp.ContainsKey(nums[i]) && i - mp[nums[i]] <= k)
111+
{
112+
return true;
113+
}
114+
mp[nums[i]] = i;
65115
}
66116
return false;
67117
}

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

+61-11
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@
4646
```python
4747
class Solution:
4848
def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
49-
helper = {}
49+
mp = {}
5050
for i, v in enumerate(nums):
51-
if v in helper and i - helper[v] <= k:
51+
if v in mp and i - mp[v] <= k:
5252
return True
53-
helper[v] = i
53+
mp[v] = i
5454
return False
5555
```
5656

@@ -59,15 +59,65 @@ class Solution:
5959
```java
6060
class Solution {
6161
public boolean containsNearbyDuplicate(int[] nums, int k) {
62-
Map<Integer, Integer> helper = new HashMap<>();
63-
for (int i = 0, n = nums.length; i < n; ++i) {
64-
if (helper.containsKey(nums[i])) {
65-
int j = helper.get(nums[i]);
66-
if (i - j <= k) {
67-
return true;
68-
}
62+
Map<Integer, Integer> mp = new HashMap<>();
63+
for (int i = 0; i < nums.length; ++i) {
64+
if (mp.containsKey(nums[i]) && i - mp.get(nums[i]) <= k) {
65+
return true;
6966
}
70-
helper.put(nums[i], i);
67+
mp.put(nums[i], i);
68+
}
69+
return false;
70+
}
71+
}
72+
```
73+
74+
### **C++**
75+
76+
```cpp
77+
class Solution {
78+
public:
79+
bool containsNearbyDuplicate(vector<int>& nums, int k) {
80+
unordered_map<int, int> mp;
81+
for (int i = 0; i < nums.size(); ++i)
82+
{
83+
if (mp.count(nums[i]) && i - mp[nums[i]] <= k) return true;
84+
mp[nums[i]] = i;
85+
}
86+
return false;
87+
}
88+
};
89+
```
90+
91+
### **Go**
92+
93+
```go
94+
func containsNearbyDuplicate(nums []int, k int) bool {
95+
mp := make(map[int]int)
96+
for i, v := range nums {
97+
if j, ok := mp[v]; ok {
98+
if i-j <= k {
99+
return true
100+
}
101+
}
102+
mp[v] = i
103+
}
104+
return false
105+
}
106+
```
107+
108+
### **C#**
109+
110+
```cs
111+
public class Solution {
112+
public bool ContainsNearbyDuplicate(int[] nums, int k) {
113+
var mp = new Dictionary<int, int>();
114+
for (int i = 0; i < nums.Length; ++i)
115+
{
116+
if (mp.ContainsKey(nums[i]) && i - mp[nums[i]] <= k)
117+
{
118+
return true;
119+
}
120+
mp[nums[i]] = i;
71121
}
72122
return false;
73123
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
bool containsNearbyDuplicate(vector<int>& nums, int k) {
4+
unordered_map<int, int> mp;
5+
for (int i = 0; i < nums.size(); ++i)
6+
{
7+
if (mp.count(nums[i]) && i - mp[nums[i]] <= k) return true;
8+
mp[nums[i]] = i;
9+
}
10+
return false;
11+
}
12+
};
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,14 @@
1-
// https://leetcode.com/problems/contains-duplicate-ii/
2-
3-
using System;
4-
using System.Collections.Generic;
5-
using System.Linq;
6-
7-
public partial class Solution
8-
{
9-
public bool ContainsNearbyDuplicate(int[] nums, int k)
10-
{
11-
//var sorted = nums.Select((n, i) => Tuple.Create(n, i)).OrderBy(t => t.Item1).ThenBy(t => t.Item2).ToList();
12-
//for (var i = 1; i < sorted.Count; ++i)
13-
//{
14-
// if (sorted[i - 1].Item1 == sorted[i].Item1 && sorted[i].Item2 - sorted[i - 1].Item2 <= k)
15-
// {
16-
// return true;
17-
// }
18-
//}
19-
//return false;
20-
21-
if (k <= 0) return false;
22-
var index = new HashSet<int>();
1+
public class Solution {
2+
public bool ContainsNearbyDuplicate(int[] nums, int k) {
3+
var mp = new Dictionary<int, int>();
234
for (int i = 0; i < nums.Length; ++i)
245
{
25-
if (index.Contains(nums[i]))
6+
if (mp.ContainsKey(nums[i]) && i - mp[nums[i]] <= k)
267
{
278
return true;
289
}
29-
if (index.Count == k)
30-
{
31-
index.Remove(nums[i - k]);
32-
}
33-
index.Add(nums[i]);
10+
mp[nums[i]] = i;
3411
}
3512
return false;
3613
}
37-
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
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+
}
8+
}
9+
mp[v] = i
10+
}
11+
return false
12+
}

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

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
class Solution {
22
public boolean containsNearbyDuplicate(int[] nums, int k) {
3-
Map<Integer, Integer> helper = new HashMap<>();
4-
for (int i = 0, n = nums.length; i < n; ++i) {
5-
if (helper.containsKey(nums[i])) {
6-
int j = helper.get(nums[i]);
7-
if (i - j <= k) {
8-
return true;
9-
}
3+
Map<Integer, Integer> mp = new HashMap<>();
4+
for (int i = 0; i < nums.length; ++i) {
5+
if (mp.containsKey(nums[i]) && i - mp.get(nums[i]) <= k) {
6+
return true;
107
}
11-
helper.put(nums[i], i);
8+
mp.put(nums[i], i);
129
}
1310
return false;
1411
}
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-
helper = {}
3+
mp = {}
44
for i, v in enumerate(nums):
5-
if v in helper and i - helper[v] <= k:
5+
if v in mp and i - mp[v] <= k:
66
return True
7-
helper[v] = i
7+
mp[v] = i
88
return False

0 commit comments

Comments
 (0)