leetcode-helper is a single-jar library that liberate you from setting up solution-test scaffold for each problem. Generate solution/test skeletons, compile, test by commands on one line. Junit 4
, log4j
, slf4j
are integrated. com.ciaoshen.leetcode.util
package provide simplest implementations for 7 commonly used data structures such as TreeNode
, ListNode
, etc.
The generated skeletons:
Unarchieve the .tar
or .tar.gz
or .zip
file. Copy everything in leetcode-helper-vX.X
folder to your work directory. If your work directory was clean, it will look like,
root
├── README.md
├── build.xml
├── demo/
│ └── two_sum/
│ └── ...
├── lib/
│ └── leetcode-helper.jar
└── problem.properties
Make sure you have installed Ant
. Use -version
option to check,
ant -version
If not, get the last version here --> 【Installing Apache Ant】
PowerShell
to launch the scriptDo NOT use cmd.exe
. It doesn't work with the paths using forward-slash("/").
To solve for example the problem "two sum", open problem.properties
file, configure the "problem.name" field as below,
problem.name = two_sum
Give your solutions a parent package name,
package.name = com.leetcode
You can also assign a method prototype to problem.members
argument (optional). If you are lazy, just keep this field blank.
problem.members = private int add(int a, int b) {}
After typing command line in console,
ant
or,
ant generate
you'll get 5 skeletons, 3 under ./src/main/java
directory, 2 in ./src/test/java
folder.
root
├── README.md
├── build.xml
├── demo/
│ └── two_sum/
│ └── ...
├── lib
│ └── leetcode-helper.jar
├── problem.properties
└── src
├── main
│ └── java
│ └── your
│ └── package
│ └── name
│ └── two_sum
│ ├── Solution.java
│ ├── Solution1.java
│ └── Solution2.java
└── test
└── java
└── your
└── package
└── name
└── two_sum
├── Tester.java
└── TesterRunner.java
Solution.java
is an interface, while Solution1.java
and Solution2.java
are two implementations. Tester.java
class works oriented to Solution
interface, thus you can easily add Solution3.java
or Solution4.java
. Make sure that they implement Solution
interface.
Tester.java
and TesterRunner.java
are JUnit module. Add your unit test in Tester.java
and launch TesterRunner
to execute JUnit test (you can launch TesterRunner in one line command).
Tester
tests Solution1
as default. To test Solution2
, just assign an instance of Solution2 to the solution
field in Tester class (do it in the Constructor of Tester class) as follow,
@BeforeClass
public static void setUpBeforeClass() throws Exception {
/* uncomment to switch solutions */
// solution = new Solution1();
solution = new Solution2();
}
To compile all above sources by one command line,
ant compile
and launch TesterRunner with another command,
ant test
or, you can do both the same time,
ant compile test
The compiled .class
files will locate in ./bin
directory as usual,
root
├── README.md
├── bin
│ └── main
│ └── java
│ └── your
│ └── package
│ └── name
│ └── two_sum
│ ├── Solution.class
│ ├── Solution1.class
│ ├── Solution2.class
│ ├── Tester.class
│ └── TesterRunner.class
├── build.xml
├── demo/
│ └── two_sum/
│ └── ...
├── lib
│ └── leetcode-helper.jar
├── problem.properties
└── src
├── main
│ └── java
│ └── your
│ └── package
│ └── name
│ └── two_sum
│ ├── Solution.java
│ ├── Solution1.java
│ └── Solution2.java
└── test
└── java
└── your
└── package
└── name
└── two_sum
├── Tester.java
└── TesterRunner.java
The following command can delete all source files, as well as .class
byte code for a specific problem,
ant clean
Make sure that you really want to do so before using this command.
-D
option to pass argumentIf you don't want to edit the problem.proberties
for every problems, you can edit the package.name
field at the first time, and pass the problem.name
argument from the command line,
ant generate -Dproblem.name three_sum
ant compile -Dproblem.name three_sum
ant test -Dproblem.name three_sum
ant clean -Dproblem.name three_sum
com.ciaoshen.leetcode.util
librarycom.ciaoshen.leetcode.util
library provide only fundamental features,
/**
* A binary tree with next pointer
* Populate each next pointer to point to its next right node.
* If there is no next right node, the next pointer should be set to NULL.
* EX:
* 1 -> NULL
* / \
* 2 -> 3 -> NULL
* / \ / \
* 4->5->6->7 -> NULL
*
* DEFINITION:
* ======================================
*
* public class TreeLinkNode {
* int val;
* TreeLinkNode left, right, next;
* TreeLinkNode(int x) { val = x; }
* }
*
* ======================================
*
*/
Related Problems:
/**
* Each node in the graph contains a label (int)
* and a list (List[UndirectedGraphNode]) of its neighbors.
* EX:
* 1
* / \
* / \
* 0 --- 2
* / \
* \_/
*
* DEFINITION:
* ===========================================================
*
* class UndirectedGraphNode {
* int label;
* List<UndirectedGraphNode> neighbors;
* UndirectedGraphNode(int x) {
* label = x;
* neighbors = new ArrayList<UndirectedGraphNode>();
* }
* };
*
* ===========================================================
*/
Related Problems:
/**
* TreeNode refers to the node of a binary tree.
* Composed of it's value and left child and right child.
* 1
* / \
* 2 3
* / \ / \
* 4 5 6 7
*
* DEFINITION:
* =================================
*
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*
* =================================
*/
/**
* ListNode is Singly-Linked List
* ex: 1->2->3->4->5
*
* DEFINITION:
* =================================
*
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*
* =================================
*/
Related Problems:
/**
Constructor is not exposed to user to create new instances. */
Related Problems:
/**
* Always used to represent a period of time
*
* DEFINITION
* ===================================================
*
* public class Interval {
* int start;
* int end;
* Interval() { start = 0; end = 0; }
* Interval(int s, int e) { start = s; end = e; }
* }
*
* ===================================================
*/
Related Problems:
/**
* The random linked list is a singly linked list given
* such that each node contains an additional random pointer
* which could point to any node in the list or null.
*
* DEFINITION
* ===================================================
*
* class RandomListNode {
* int label;
* RandomListNode next, random;
* RandomListNode(int x) { this.label = x; }
* };
*
* ===================================================
*/
Related Problem:
/**
* Binary Search Tree looks like:
* 4
* / \
* 2 5
* / \
* 1 3
*
* DEFINITION
* ===================================================
*
* class Node {
* public int val;
* public Node left, right;
*
* public Node() {}
* public Node(int v, Node l, Node r) {
* val = v; left = l; right = r;
* }
* }
*
* ===================================================
*/
Related Problems:
com.ciaoshen.leetcode.util
library to support the commonly used data structures in leetcode.java.io
to java.nio
build.xml
scaffoldcom.ciaoshen.leetcode.util
package