leetcode-helper

repository·master·Indexed 43 days ago

https://github.com/helloshen/leetcode-helper

A single-jar utility for automating boilerplate setup for LeetCode problem solving in Java. It uses Apache Ant to generate solution interfaces, implementation skeletons, and JUnit test suites. It includes the com.ciaoshen.leetcode.util library providing common data structures such as TreeNode, ListNode, UndirectedGraphNode, and NestedInteger.

Tokens
4.1K
Snippets
29
Records
31
Agent score
56%

What's inside leetcode-helper

  1. How to switch between different solutions in Tester.java

    master

    The generated Tester.java is designed to test the Solution interface. By default, it tests Solution1. To test a different implementation (like Solution2), you must manually update the instance assigned to the solution field within the @BeforeClass setup method in Tester.java.

        @BeforeClass
        public static void setUpBeforeClass() throws Exception {
            /* uncomment to switch solutions */
            // solution = new Solution1();
            solution = new Solution2();
        }
    @BeforeClass
        public static void setUpBeforeClass() throws Exception {
            /* uncomment to switch solutions */
            // solution = new Solution1();
            solution = new Solution2();
        }
  2. Install leetcode-helper

    master

    To install, unarchive the .tar, .tar.gz, or .zip file provided. Copy the entire contents of the leetcode-helper-vX.X folder into your working directory.

    A clean working directory should contain the following structure:

    root
    ├── README.md
    ├── build.xml
    ├── lib
    │   └── leetcode-helper.jar
    └── problem.properties
    root
    ├── README.md
    ├── build.xml
    ├── lib
    │   └── leetcode-helper.jar
    └── problem.properties
  3. Override problem configuration via CLI arguments

    master

    Instead of editing problem.properties for every new problem, you can pass the problem.name directly as a property using the -D flag. This allows you to keep a static package.name in your properties file while switching problems quickly.

    Examples:

    ant generate -Dproblem.name three_sum
    ant compile -Dproblem.name three_sum
    ant test -Dproblem.name three_sum
    ant clean -Dproblem.name three_sum
    ant generate -Dproblem.name three_sum
  4. Compile and run JUnit tests

    master

    Once your solutions are implemented, use the following Ant commands to manage the lifecycle:

    • Compile all sources: ant compile or ant compile -Dproblem.name <name>
    • Run tests: ant test or ant test -Dproblem.name <name>
    • Compile and run tests in one step: ant compile test or ant compile test -Dproblem.name <name>
    • Clean up: ant clean deletes all source files and .class files for the current problem.

    Switching between implementations: By default, Tester.java tests Solution1. To test a different implementation (e.g., Solution2), modify the @BeforeClass setup in Tester.java:

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        /* uncomment to switch solutions */
        // solution = new Solution1();
        solution = new Solution2();
    }
    ant compile test
  5. Generate solution and test skeletons

    master

    Use Apache Ant to generate the necessary Java files for a problem. Running ant or ant generate creates a directory structure containing an interface (Solution.java), multiple implementations (Solution1.java, Solution2.java), and JUnit test files (Tester.java, TesterRunner.java).

    To avoid editing problem.properties every time, you can pass the problem.name as a command-line argument using the -D flag.

    ant generate -Dproblem.name three_sum
  6. Configure problem information in problem.properties

    master

    Before generating skeletons, you must configure the problem details in the problem.properties file.

    Key configuration fields:

    • problem.name: The name of the LeetCode problem (e.g., two_sum).
    • package.name: The parent package name for your Java classes (e.g., com.leetcode).
    • problem.members (optional): A method prototype for the solution (e.g., private int add(int a, int b) {}). If left blank, the generator uses default settings.
    problem.name = two_sum
    package.name = com.leetcode
    problem.members = private int add(int a, int b) {}
  7. Configure a problem in problem.properties

    master

    Before generating skeletons, configure the problem.properties file with the problem name and your desired Java package name.

    Key fields:

    • problem.name: The identifier for the problem (e.g., two_sum).
    • package.name: The Java package where your code will reside (e.g., com.ciaoshen.leetcode).
    • problem.members (optional): You can provide a method prototype here to pre-populate the solution structure. If left blank, the skeleton will be generated without specific method signatures.

    Example configuration:

    problem.name = two_sum
    package.name = com.ciaoshen.leetcode
    problem.members = private int add(int a, int b) {}
  8. Initialize the project environment with the init script

    master

    The init script sets up the environment for a specific LeetCode problem by loading configuration from a problem.properties file or via command-line arguments. It determines the problem name, package structure, utility libraries, and method signatures (members) to prepare the project layout.

    Configuration Sources

    1. problem.properties file: The script looks for a file named problem.properties in the current directory. It parses the following keys (note that dots in keys are converted to underscores internally):

      • problem.name: The name of the problem.
      • problem.pck: The Java package name (e.g., com.example.solution).
      • problem.util: The utility library to be used.
      • problem.members: The method signatures/members to include.
    2. Command-line arguments: You can override the properties file using the following flags:

      • -name <value>: Sets the problem name.
      • -pck <value>: Sets the package name.
      • -lib <value>: Sets the utility library.

    Default Behavior

    If problem.members is not specified in the properties file, the script defaults the members to //your-method().

    # Example: Using command line arguments to override properties
    ./init -name TwoSum -pck com.leetcode.solution -lib myutils
  9. Use the com.ciaoshen.leetcode.util library data structures

    master
    The com.ciaoshen.leetcode.util library provides fundamental data structures commonly used in LeetCode problems. These classes include member fields, constructors, and toString() serialization to facilitate solving problems without redefining standard structures.
  10. Reference: com.ciaoshen.leetcode.util data structures

    master

    The com.ciaoshen.leetcode.util library provides common LeetCode data structure definitions to simplify problem solving.

    Available structures include:

    • TreeLinkNode: A binary tree where each node has a next pointer to its right neighbor.
    • UndirectedGraphNode: A graph node with a label and a list of neighbors.
    • TreeNode: A standard binary tree node.
    • ListNode: A singly-linked list node.
    • NestedInteger: An interface for handling nested integer lists.
    • Interval: Represents a time period with start and end fields.
    • RandomListNode: A singly-linked list node with an additional random pointer.
    • TreeNode (standard binary tree node).