Repository navigation
Add solution for "The Two Sneaky Numbers of Digitville" with unit tests. #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // Tags: Array, Hash Set | ||
| package com.leetcode.easy; | ||
|
|
||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
|
|
||
| public class TheTwoSneakyNumbersOfDigitville3289 { | ||
| /** | ||
| * Finds the two numbers that appear twice in the array. | ||
| * <p> | ||
| * Time Complexity: O(n) where n is the length of the array. | ||
| * We iterate through the array once, and HashSet operations (add, contains) are O(1) on average. | ||
| * <p> | ||
| * Space Complexity: O(n) for the HashSet to store seen numbers. | ||
| * In the worst case, we store almost all numbers before finding the duplicates. | ||
| */ | ||
| public int[] getSneakyNumbers(int[] nums) { | ||
| int[] res = new int[2]; | ||
| Set<Integer> seen = new HashSet<>(); | ||
| int index = 0; | ||
| for (int num : nums) { | ||
| if (seen.contains(num)) { | ||
| res[index] = num; | ||
| if (index == 1) { | ||
| break; | ||
| } | ||
| index++; | ||
| } else { | ||
| seen.add(num); | ||
| } | ||
| } | ||
| return res; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package com.leetcode.easy; | ||
|
|
||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.Arrays; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||
|
|
||
| public class TheTwoSneakyNumbersOfDigitville3289Test { | ||
| private TheTwoSneakyNumbersOfDigitville3289 solution; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| solution = new TheTwoSneakyNumbersOfDigitville3289(); | ||
| } | ||
|
|
||
| @Test | ||
| void testGetSneakyNumbers_Example1() { | ||
| int[] nums = {0, 1, 1, 0}; | ||
| int[] expected = {0, 1}; | ||
|
|
||
| int[] result = solution.getSneakyNumbers(nums); | ||
| Arrays.sort(result); | ||
|
|
||
| assertArrayEquals(expected, result); | ||
| } | ||
|
|
||
| @Test | ||
| void testGetSneakyNumbers_Example2() { | ||
| int[] nums = {0, 3, 2, 1, 3, 2}; | ||
| int[] expected = {2, 3}; | ||
|
|
||
| int[] result = solution.getSneakyNumbers(nums); | ||
| Arrays.sort(result); | ||
|
|
||
| assertArrayEquals(expected, result); | ||
| } | ||
|
|
||
| @Test | ||
| void testGetSneakyNumbers_Example3() { | ||
| int[] nums = {7, 1, 5, 4, 3, 4, 6, 0, 9, 5, 8, 2}; | ||
| int[] expected = {4, 5}; | ||
|
|
||
| int[] result = solution.getSneakyNumbers(nums); | ||
| Arrays.sort(result); | ||
|
|
||
| assertArrayEquals(expected, result); | ||
| } | ||
|
Comment on lines
+18
to
+49
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentation style is inconsistent with the rest of the codebase. Other solution files in this repository use multi-line block comments with /* / format for complexity analysis, rather than Javadoc-style /* */ comments. Consider changing to the standard block comment format used in files like ContainsDuplicate217.java and TwoSum1.java for consistency.