Skip to content

luangong/leetcode-java

Repository files navigation

Solutions to LeetCode Problems in Java

Quickstart

First, ensure JDK 21 is installed, then run ./mvnw test (mvnw test on Windows):

./mvnw test

You can also build and test with Bazel:

bazel test //...

Bazel automatically downloads JDK 21 from the Internet to compile and run your code so you don’t have to install it locally. You may still need to install JDK 21 locally in order to browse the code in an IDE, though.

Runtime Environment

As stated in What are the environments for the programming languages:

  • LeetCode compiles and runs submitted Java code with JDK 21.
  • Java 8 features such as lambda expressions and the Streams API can be used.
  • Most standard library classes are already imported automatically for convenience.
  • The javafx.util.Pair class is also included.

Unit Testing

This repo uses JUnit 5 parameterized testing feature to load test cases from external YAML files. The reason of externalizing test data is to make it easy for projects written in different languages to share test data.

For each problem, there is a YAML file with a path like src/test/resources/<problem_slug>.yml, where <problem_slug> is the title of the problem in lowercase and spaces replaced with underscores (e.g., src/test/resources/two_sum.yml).

Updating Maven Dependencies

  • If you use Maven to build and test the project, you can run mvn versions:display-dependency-updates to check if there are outdated dependencies and then manually edit pom.xml if there are any.

  • If you use Bazel to build and test the project, you can run bazel run @mvn//:outdated to see if there are any outdated dependencies from Maven. After updating dependencies by manually editing WORKSPACE, you should run REPIN=1 bazel run @unpinned_maven//:pin and commit maven_install.json.

TODOs

  • Put test cases files directly under src/test/resources rather than in the problem-specific directory (i.e., put test cases in src/test/resources/two_sum.json rather than src/test/resources/two_sum/data.json)
  • Add support for loading test cases from YAML files because:
    • YAML is more concise
    • YAML is a superset of JSON, so any valid JSON is also valid YAML
    • YAML supports adding comments
  • Take a look at eo-yaml and Jackson Data Format YAML for handling YAML files
    • eo-yaml Doesn’t support flow sequences and flow mappings
    • Jackson Data Format YAML is based on SnakeYAML Engine
  • Add the ability to show code coverage
  • Remove JSON-related code and dependencies
  • Enable Bzlmod
  • Try Gazelle to see if we can generate BUILD.bazel files automatically
  • Improve CI performance by using

Related Projects

About

Java solutions to LeetCode problems

Resources

Stars

Watchers

Forks