Leetcode
Code snippets and Test cases for Leetcode problems.
Introduction
This project hosts the code and test cases for Leetcode problems. Besides, some of the interesting and amazing solutions from the discussion are also included.
Environment
This project is coded and tested with Java 8 and Gradle 5.4.1.
The test cases are based on the TestNG framework (6.14.3).
The Google Java code style is used in this project.
Code Layout
-
src/main/java/com/leetcode/snippets
holds the original blank code snippets fromleetcode.com
and you may start solving the problems from here. -
src/main/java/com/leetcode/discussion
holds some of the solutions provided by the genius in Leetcode Disucss. -
src/main/java/cn/hjmao/leetcode
holds all my leetcode solutions. The code are in the same code format as the leetcode requires, so it can be directly copied and pasted on the leetcode OJ input box. -
src/test/java/cn/hjmao/leetcode
holds the test cases. You may copy the test cases to yours and just focus on thinking about the core algorithms for the problems. You can execute all the test cases withgradle test
. If you have more test cases, you can append the test cases in the test unit files. -
src/main/java/cn/hjmao/utils
contains some of the utilities for my solutions.
Source Code Layout
src
├── main
│ └── java
│ ├── cn
│ │ └── hjmao
│ │ ├── ProblemFetcher.java
│ │ ├── leetcode
│ │ │ ├── a001twosum
│ │ │ │ └── Solution.java
│ │ │ ├── a002addtwonumbers
│ │ │ │ └── Solution.java
│ │ │ ├── ...
│ │ │ │ └── Solution.java
│ │ │ └── ...
│ │ │ └── Solution.java
│ │ └── utils
│ │ ├── AssertUtils.java
│ │ ├── list
│ │ │ ├── ListNode.java
│ │ │ └── ListNodeUtils.java
│ │ └── tree
│ │ ├── TreeNode.java
│ │ └── TreeNodeUtils.java
│ └── com
│ └── leetcode
│ ├── discussion
│ │ ├── a001twosum
│ │ │ └── Solution.java
│ │ ├── a002addtwonumbers
│ │ │ └── Solution.java
│ │ ├── ...
│ │ │ └── Solution.java
│ │ └── ...
│ │ └── Solution.java
│ └── snippets
│ ├── a001twosum
│ │ └── Solution.java
│ ├── a002addtwonumbers
│ │ └── Solution.java
│ ├── ...
│ │ └── Solution.java
│ └── ...
│ └── Solution.java
└── test
└── java
└── cn
└── hjmao
└── leetcode
├── a001twosum
│ └── SolutionTest.java
├── a002addtwonumbers
│ └── SolutionTest.java
├── ...
│ └── SolutionTest.java
└── ...
└── SolutionTest.java
How to use this repo
-
Install Gradle 5.4.1
-
Clone this project
git clone https://github.com/huajianmao/leetcode
-
Install the dependencies.
gradle check
-
Create your own code package in
src/main/java
for code andsrc/test/java
for test cases, for exampleyour.code.place
-
Choose a problem and move the corresponding blank snippet code from
src/main/java/com/leetcode/snippets
, so that you do not need to prepare theSolution
file from scratch. Just focus on problem solving!!! For example, copy the packagea001twosum
to packageyour.code.place
. If you are using IDE like Jetbrains IDEA, it would automatically change the package name to yours, otherwise you might have to revise the snippet manually. -
Copy the test cases code in
cn.hjmao.leetcode
toyour.code.place
package in thesrc/test/java
. NOTICES: If you are using the IDEs, as the IDE would probably insert theimport cn.hjmao.leetcode.xxx.Solution
to the test case code file, you might need to delete it manually. -
Focus and write the algorithm in the code file in
src/main/java/your/code/place/Solution.java
. -
Run and debug with the copied test cases in
src/main/java/your/code/place/Solution.java
.
ProblemFetcher
script
If a new problem is provided in leetcode.com
, you might use ProblemFetcher
to automatically get the problem descriptions and the Solution
file will be generated in com.leetcode.snippets
,
and you can copy it to your code place package.
But in this case, you have to write the test cases by yourself.
Notice: You might need to revise the snippet slightly to make it return in the correct type.
Contribution
It would be my honor if you like this repo. You are very welcome to submit PRs on solutions, test cases. There would probably have lots of issues in this project, if you find some one, please throw a issue to me. Please help improving this repo better. Thanks!