Build the project:
./gradlew buildRun example tests:
./gradlew testleetcode-java/
├── src/
│ ├── main/
│ │ └── java/org/leetcode/ # Solution implementations
│ └── test/
│ └── java/org/leetcode/ # Unit tests
├── build.gradle # Build configuration
├── gradle/
│ └── wrapper/ # Gradle wrapper files
└── settings.gradle # Project settings
Create package for your problem (e.g., "twosum"):
mkdir -p src/main/java/org/leetcode/twosum
mkdir -p src/test/java/org/leetcode/twosumImplement your solution:
// src/main/java/org/leetcode/twosum/Solution.java
package org.leetcode.twosum;
import java.util.HashMap;
import java.util.Map;
public class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> numMap = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int complement = target - nums[i];
if (numMap.containsKey(complement)) {
return new int[]{numMap.get(complement), i};
}
numMap.put(nums[i], i);
}
throw new IllegalArgumentException("No solution found");
}
}Create corresponding tests:
// src/test/java/org/leetcode/twosum/SolutionTest.java
package org.leetcode.twosum;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class SolutionTest {
private final Solution solution = new Solution();
@Test
public void testTwoSumBasicCase() {
int[] result = solution.twoSum(new int[]{2,7,11,15}, 9);
assertArrayEquals(new int[]{0,1}, result);
}
@Test
public void testNoSolution() {
assertThrows(IllegalArgumentException.class, () -> {
solution.twoSum(new int[]{2,7,11,15}, 100);
});
}
}| Command | Description |
|---|---|
./gradlew build |
Build the project |
./gradlew test |
Run all tests |
./gradlew clean |
Clean build artifacts |
./gradlew test --tests org.leetcode.* |
Run specific test class |
- File → Open → Select
build.gradle - Import as Gradle project
- Enable auto-import
- Install Java Extension Pack
- Open project folder
- Trust Gradle wrapper
- Fork the repository
- Create a new branch:
git checkout -b feature/solution-for-problem-xyz- Add your solution with tests
- Commit your changes:
git commit -m "Add solution for Problem XYZ"- Push to your fork:
git push origin feature/solution-for-problem-xyz- Create a Pull Request
| Problem | Solution | Tests | Difficulty |
|---|---|---|---|
| Two Sum | ✅ | ✅ | Easy |
| Add Two Numbers | ❌ | ❌ | Medium |
| Longest Substring | ❌ | ❌ | Medium |
MIT License - See LICENSE for details.
- Inspired by LeetCode's problem set
- Built with Gradle and JUnit
- Community contributions welcome