My LeetCode solutions in Java, focused on clean code and optimal algorithms.
| # | Problem | Difficulty | Time | Space |
|---|---|---|---|---|
| 1 | Two Sum | Easy | O(n log n) |
O(n) |
| 9 | Palindrome Number | Easy | O(log10(n) / 2) |
O(1) |
| 1653 | Minimum Deletions to Make String Balanced | Medium | O(n) |
O(n) |
src/main/java/codes/yam/leetcode/{problem-slug}/
Solution.java # Solution implementation
package-info.java # Problem metadata
src/test/java/codes/yam/leetcode/{problem-slug}/
SolutionTest.java # Unit tests
SolutionBenchmark.java # JMH benchmarks
mvn test # Run all tests
mvn test -Dtest=SolutionTest # Run a specific test class
mvn javadoc:javadoc # Generate JavadocHow to add a new LeetCode solution using test-driven development.
Right-click src/test/java/codes/yam/leetcode/ → New → Package → problemslug, then New → Java Class →
SolutionTest.
Start with LeetCode's example cases plus a few edge cases (aim for 3–5 per behavior):
For single-arg problems — use @ValueSource, grouped by expected outcome:
class SolutionTest {
private final Solution solution = new Solution();
@ParameterizedTest(name = "{0} is a palindrome")
@ValueSource(ints = {121, 0, 1, 1221, 12321})
void testPalindromes(int x) {
assertTrue(solution.isPalindrome(x));
}
@ParameterizedTest(name = "{0} is not a palindrome")
@ValueSource(ints = {-121, 10, 123})
void testNonPalindromes(int x) {
assertFalse(solution.isPalindrome(x));
}
}For multi-arg problems — use @MethodSource:
class SolutionTest {
private final Solution solution = new Solution();
static Stream<Arguments> cases() {
return Stream.of(
arguments(new int[]{2, 7, 11, 15}, 9, new int[]{0, 1}), // basic case
arguments(new int[]{3, 2, 4}, 6, new int[]{1, 2}), // not-first-pair
arguments(new int[]{3, 3}, 6, new int[]{0, 1}), // duplicates
arguments(new int[]{-1, -2, -3, -4, -5}, -8, new int[]{2, 4}) // negatives
);
}
@ParameterizedTest
@MethodSource("cases")
void twoSum(int[] nums, int target, int[] expected) {
assertArrayEquals(expected, solution.twoSum(nums, target));
}
}Solution will be red (unresolved). Put your cursor on it and press ⌥⏎ → Create class 'Solution'. Change the
target directory to src/main/java — IntelliJ creates the package automatically.
Then ⌥⏎ on the red method call → Create method — IntelliJ infers the signature from your test args.
- Red: Run the test with ⌃⇧R — confirm it fails
- Green: Write the minimal code to pass all cases
- Refactor: Clean up, then re-run to verify
- Add
package-info.javain the main source package (Right-click → New → File) - Add Javadoc with time/space complexity to
Solution.java - Ensure
Solutionis package-scoped (nopublicmodifier)
mvn test -Dtest=SolutionTest| Shortcut | Action |
|---|---|
| ⌥⏎ | Quick fix (create class/method from usage) |
| ⌃⇧R | Run nearest test |
| ⇧⌘T | Toggle between class and its test |
- Java 25 (LTS)
- JUnit 6 for parameterized testing
- JMH for benchmarking with GC profiling
- Maven for builds
Yam Borodetsky 💻 📖 |
Houda Belhad 🚇 |