Skip to content

Commit bfbb9bc

Browse files
committed
feat: add 219 Contains Duplicate 2
1 parent e9d11de commit bfbb9bc

File tree

6 files changed

+71
-29
lines changed

6 files changed

+71
-29
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Medium: https://medium.com/@nphausg
3636
| [0027. Remove Element](https://leetcode.com/problems/remove-element) | 🔥 | [Solution](src/com/nphausg/leetcode/easy/RemoveElement.java) | - |
3737
| [0121. Best time to buy and sell stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock) | 🔥 | - | [Solution](src/com/nphausg/leetcode/easy/BuyAndSellStock.kt) |
3838
| [0217. Contains Duplicate](https://leetcode.com/problems/contains-duplicate) | 🔥 | [Solution](src/com/nphausg/leetcode/easy/ContainsDuplicate.java) | - |
39+
| [0219. Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii) | 🔥 | [Solution](src/com/nphausg/leetcode/easy/ContainsDuplicate2.java) | - |
3940
| [1929. Concatenation Of Array](https://leetcode.com/problems/concatenation-of-array) | 🔥 | [Solution](src/com/nphausg/leetcode/easy/ConcatenationArray.java) | - |
4041
| [0017. Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number) | 🔥🔥 | [Solution](src/com/nphausg/leetcode/medium/LetterCombinations.java) | - |
4142
| [0091. Decode Ways ](https://leetcode.com/problems/decode-ways) | 🔥🔥 | [Solution](src/com/nphausg/leetcode/medium/DecodeWays.java) | - |

leetcode.solution.iml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,15 @@
88
<orderEntry type="inheritedJdk" />
99
<orderEntry type="sourceFolder" forTests="false" />
1010
<orderEntry type="library" name="KotlinJavaRuntime" level="project" />
11+
<orderEntry type="module-library">
12+
<library name="JUnit4">
13+
<CLASSES>
14+
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.13.1/junit-4.13.1.jar!/" />
15+
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
16+
</CLASSES>
17+
<JAVADOC />
18+
<SOURCES />
19+
</library>
20+
</orderEntry>
1121
</component>
1222
</module>

src/com/.DS_Store

0 Bytes
Binary file not shown.

src/com/nphausg/leetcode/Main.java

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.nphausg.leetcode.config;
2+
3+
public class BaseTest {
4+
private static int count;
5+
6+
@org.junit.BeforeClass
7+
public static void beforeClass() {
8+
System.out.println("Running a test...");
9+
System.out.println("This executes before any test cases. Count = " + count++);
10+
}
11+
12+
13+
@org.junit.AfterClass
14+
public static void afterClass() {
15+
System.out.println("This executes after any test cases. Count = " + count++);
16+
}
17+
18+
@org.junit.After
19+
public void teardown() {
20+
System.out.println("Count = " + count++);
21+
}
22+
23+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.nphausg.leetcode.easy;
2+
3+
import com.nphausg.leetcode.config.BaseTest;
4+
5+
import java.util.HashSet;
6+
7+
import static org.junit.Assert.assertFalse;
8+
import static org.junit.Assert.assertTrue;
9+
10+
/**
11+
* <a href="https://leetcode.com/problems/contains-duplicate-ii">Contains duplicate II</a>
12+
*/
13+
public class ContainsDuplicate2 {
14+
public static boolean containsNearbyDuplicate(int[] nums, int k) {
15+
HashSet<Integer> set = new HashSet<Integer>();
16+
for (int i = 0; i < nums.length ; i ++){
17+
if(set.contains(nums[i])){
18+
return true;
19+
}
20+
set.add(nums[i]);
21+
if(set.size() > k){
22+
set.remove(nums [i - k]);
23+
}
24+
}
25+
return false;
26+
}
27+
28+
public static class ContainsDuplicate2Test extends BaseTest {
29+
30+
@org.junit.Test
31+
public void testCases() {
32+
//assertTrue(ContainsDuplicate2.containsDuplicate(new int[]{1,0,1,1}, 1));
33+
//assertTrue(ContainsDuplicate2.containsDuplicate(new int[]{1, 2, 3, 1}, 3));
34+
assertFalse(ContainsDuplicate2.containsNearbyDuplicate(new int[]{1, 2, 3, 1, 2, 3}, 2));
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)