Skip to content

ameen-pj/leetcode4j

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Leetcode4j ⚡

A simple Leetcode testing library for Java.

Advantages ✨

  • Use your favourite IDE
  • Multiple utility classes to save time and assist users in solving problems more efficiently.
  • Debug your solutions locally without subscribing to 'LeetCode Premium'.

Installation ⬇️

  1. Clone this project
  2. Make sure that you have maven installed and run the following command
>>> cd leetcode4j/
>>> mvn clean install
  1. Create a new maven project and add the project to your pom.xml file
<dependency>
    <groupId>com.apj.projects</groupId>
    <artifactId>leetcode4j</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>
  1. Now you are all set to use Leetcode4j !!

Manual 📖

  1. Create an entry point for your application and configure the package name which contains the leetcode problems.

App.java

import com.apj.projects.leetcode4j.core.LeetcodeProblemManager;

public class App {
    public static void main(String[] args) {
        LeetcodeProblemManager mgr = new LeetcodeProblemManager("PCKG_NAME");
        mgr.runTestCases();
    }
}

  1. Create a problem class and annotate the class with @LeetcodeProblem and the testcase methods with @TestCase

Eg: com.leetcode.problems.validBST.java

package com.leetcode.problems;

import java.util.LinkedList;

import com.apj.projects.leetcode4j.annotations.LeetcodeProblem;
import com.apj.projects.leetcode4j.annotations.TestCase;
import com.apj.projects.leetcode4j.datatypes.TreeNode;
import com.apj.projects.leetcode4j.datatypes.utils.LeetcodeInputTransformer;
import com.apj.projects.leetcode4j.datatypes.utils.TreeNodeUtility;

@LeetcodeProblem
public class ValidBST {

	@TestCase
	public void T1() {
		System.out.println(new ValidBST().isValidBST(new LeetcodeInputTransformer("[2, 1, 3]").transformToIntegerBST()));
	}
	@TestCase
	public void T2() {
		System.out.println(new ValidBST().isValidBST(new LeetcodeInputTransformer("[5, 1, 4, null, null, 3, 6]").transformToIntegerBST()));
	}
	@TestCase
	public void T3() {
		System.out.println(new ValidBST().isValidBST(new LeetcodeInputTransformer("[2,2,2]").transformToIntegerBST()));
	}

	// CODE
	private LinkedList<Integer> stack = new LinkedList<Integer>();

	public boolean isValidBST(TreeNode root) {
		if (root == null)
			return true;
		boolean b1 = isValidBST(root.left);
		if (stack.size() > 0 && stack.peek() >= root.val)
			return false;
		stack.addFirst(root.val);
		boolean b2 = isValidBST(root.right);
		System.out.println(stack);
		return b1 && b2;
	}
}
  1. Run App.java

Datatypes ℹ️

  1. ListNode
  2. TreeNode

(see javadoc for more details)

LeetcodeInputTransformer 💪

A powerful class to transform Leetcode input strings to multiple datatypes.

NOTE: This class is under active development

eg:

(com.apj.projects.leetcode4j.datatypes.utils.LeetcodeInputTransformer)

LeetcodeInputTransformer lit = new LeetcodeInputTransformer("[1,2,3]");
int[] intArr = lit.transformTo1DIntArray();
ListNode head = lit.transformToIntLinkedList();
TreeNode root = lit.transformToIntegerBST();
String[] strArr = lit.transformTo1DStringArray();

(see javadoc for more details)

About

A simple Leetcode testing library for Java

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages