FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode/java/219_Contains_Duplicate_II.java at master · jakehoare/leetcode · GitHub
jakehoare
/
leetcode
Public
Notifications
You must be signed in to change notification settings
Fork
30
Star
51
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
leetcode
/
java
/
219_Contains_Duplicate_II.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
23 lines (21 loc) · 784 Bytes
Breadcrumbs
leetcode
/
java
/
219_Contains_Duplicate_II.java
Copy path
File metadata and controls
23 lines (21 loc) · 784 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
https://leetcode.com/problems/contains-duplicate-ii/
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.
Store seen values in window, remove elements earlier than window.
Time - O(n)
Space - O(n)
*/
public
class
Solution
{
public
boolean
containsNearbyDuplicate
(
int
[]
nums
,
int
k
) {
Set
<
Integer
>
set
=
new
HashSet
<
Integer
>();
for
(
int
i
=
0
;
i
<
nums
.
length
; ++
i
) {
if
(!
set
.
add
(
nums
[
i
]))
// returns false if already in set
return
true
;
if
(
i
-
k
>=
0
)
// remove elements outside window
set
.
remove
(
nums
[
i
-
k
]);
}
return
false
;
}
}
Back
|
FazBrowse Home
|
New Git URL