项目作者: dragonlayout

项目描述 :
Generate leetcode problem scaffold code with maven-plugin
高级语言: Java
项目地址: git://github.com/dragonlayout/leetcode-helper-maven-plugin.git


leetcode-maven-plugin

This plugin will let you focus on the leetcode problem’s solutions and generate Solution and Test scaffold code automatically for you.

User Guide

  1. Download the project, and run mvn clean install
  2. Add leetcode-maven-plugin to your pom.xml plugins
    1. <build>
    2. <plugins>
    3. <plugin>
    4. <groupId>com.dragonlayout</groupId>
    5. <artifactId>leetcode-maven-plugin</artifactId>
    6. <version>1.5-SNAPSHOT</version>
    7. </plugin>
    8. </plugins>
    9. </build>
  3. Create problem.properties file in your resources folder, for example:
    1. package.name=com.dragonlayout.leetcode.solutions
    2. problem.category=easy
    3. problem.no=0001
    4. problem.name=Two Sum
    5. problem.description=\
    6. Given an array of integers, return indices of the two numbers such that they add up to a specific target.\n\
    7. You may assume that each input would have exactly one solution, and you may not use the same element twice.\n\
    8. \n\
    9. Example:\n\
    10. \n\
    11. Given nums = [2, 7, 11, 15], target = 9,\n\
    12. \n\
    13. Because nums[0] + nums[1] = 2 + 7 = 9,\n\
    14. return [0, 1].
    15. problem.method.structure=public int[] twoSum(int[] nums, int target) {}
    you should add \n\ at the end of each line of the problem.description.
  4. Execute mvn leetcode:generateSolution in your leetcode-solution project will generate one Solution.java(interface), Solution1.java and SolutionTest.java. If you want to add another solution, such as Solution2.java. Just execute the mvn leetcode:generateSolution again will generate Solution2.java and add Solution2 field member to SolutionTest.java.

    Now you can see the Solution and Test file in your project like this,

    1. -src
    2. -main
    3. -java
    4. -package.name
    5. -problem.category
    6. -_problem.no_problem.name
    7. -Solution.java
    8. -Solution1.java
    9. -test
    10. -java
    11. -package.name
    12. -problem.category
    13. -_problem.no_problem.name
    14. -SolutionTest.java
    1. package com.dragonlayout.leetcode.solutions.easy._0167_two_sum_ii;
    2. public interface Solution {
    3. public int[] twoSum(int[] numbers, int target);
    4. }

    ```java
    package com.dragonlayout.leetcode.solutions.easy._0001_two_sum;

    // Date: 2019/09/08 15:43:14

    // Given an array of integers, return indices of the two numbers such that they add up to a specific target.
    // You may assume that each input would have exactly one solution, and you may not use the same element twice.

    // Example:

    // Given nums = [2, 7, 11, 15], target = 9,

    // Because nums[0] + nums[1] = 2 + 7 = 9,
    // return [0, 1].

  1. // Time complexity:
  2. // Space complexity:
  3. // Notes:
  4. public class Solution1 implements Solution {
  5. @Override
  6. public int[] twoSum(int[] nums, int target) {}
  7. }
  8. ```
  9. ```java
  10. package com.dragonlayout.leetcode.solutions.easy._0001_two_sum;
  11. import org.junit.Before;
  12. import org.junit.Test;
  13. import org.junit.runner.RunWith;
  14. import org.junit.runners.Parameterized;
  15. import org.junit.runners.Parameterized.Parameters;
  16. import java.util.Arrays;
  17. import java.util.Collection;
  18. import static org.hamcrest.CoreMatchers.equalTo;
  19. import static org.hamcrest.CoreMatchers.is;
  20. import static org.junit.Assert.assertThat;
  21. @RunWith(Parameterized.class)
  22. public class SolutionTest {
  23. private int[] nums;
  24. private int target;
  25. private int[] expected;
  26. private Solution solution;
  27. public SolutionTest(int[] nums, int target, int[] expected) {
  28. this.nums = nums;
  29. this.target = target;
  30. this.expected = expected;
  31. }
  32. @Parameters
  33. public static Collection<Object[]> testCases() {
  34. return Arrays.asList(new Object[][]{
  35. // todo add your test cases here
  36. });
  37. }
  38. @Before
  39. public void setUp() throws Exception {
  40. solution = new Solution1();
  41. }
  42. @Test
  43. public void twoSum() {
  44. int[] actual = solution.twoSum(nums, target);
  45. assertThat(actual, is(equalTo(expected)));
  46. }
  47. }
  48. ```
  1. Add your test cases to the SolutionTest.testCases() and everything is done.