File tree Expand file tree Collapse file tree 1 file changed +21
-1
lines changed Expand file tree Collapse file tree 1 file changed +21
-1
lines changed Original file line number Diff line number Diff line change @@ -51,7 +51,7 @@ https://leetcode-cn.com/problems/contains-duplicate-ii/
51
51
52
52
## 代码
53
53
54
- * 语言支持:JS,Python,C++
54
+ * 语言支持:JS,Python,C++, Java
55
55
56
56
Javascript Code:
57
57
@@ -86,6 +86,7 @@ class Solution:
86
86
d[num] = index
87
87
return False
88
88
```
89
+
89
90
C++ Code:
90
91
``` C++
91
92
class Solution {
@@ -106,6 +107,25 @@ public:
106
107
};
107
108
```
108
109
110
+ Java Code:
111
+
112
+ ```java
113
+ class Solution {
114
+ public boolean containsNearbyDuplicate(int[] nums, int k) {
115
+ Map<Integer, Integer> map = new HashMap<>();
116
+ for(int i=0;i<nums.length;i++)
117
+ {
118
+ if(map.get(nums[i]) != null && (i-map.get(nums[i])) <= k)
119
+ {
120
+ return true;
121
+ }
122
+ map.put(nums[i], i);
123
+ }
124
+ return false;
125
+ }
126
+ }
127
+ ```
128
+
109
129
** 复杂度分析**
110
130
- 时间复杂度:$O(N)$
111
131
- 空间复杂度:$O(N)$
You can’t perform that action at this time.
0 commit comments