Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加几个常用的工具类,用来做单机的测试非常方便 #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,19 @@
<artifactId>leetcode-question</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>15</maven.compiler.source>
<maven.compiler.target>15</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.69</version>
</dependency>
</dependencies>

</project>
51 changes: 51 additions & 0 deletions src/main/java/com/shuzijun/leetcode/ListNodeUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.shuzijun.leetcode;

/**
* @author : CAOMU
* @version : 1.0
* @since : 2021/02/19, Fri, 10:09
*/
public class ListNodeUtils {
public static int[] stringToIntegerArray(String input) {
input = input.trim();
input = input.substring(1, input.length() - 1);
if (input.length() == 0) {
return new int[0];
}

String[] parts = input.split(",");
int[] output = new int[parts.length];
for (int index = 0; index < parts.length; index++) {
String part = parts[index].trim();
output[index] = Integer.parseInt(part);
}
return output;
}

public static ListNode stringToListNode(String input) {
// Generate array from the input
int[] nodeValues = stringToIntegerArray(input);

// Now convert that list into linked list
ListNode dummyRoot = new ListNode(0);
ListNode ptr = dummyRoot;
for (int item : nodeValues) {
ptr.next = new ListNode(item);
ptr = ptr.next;
}
return dummyRoot.next;
}

public static void prettyPrintLinkedList(ListNode node) {
while (node != null && node.next != null) {
System.out.print(node.val + "->");
node = node.next;
}

if (node != null) {
System.out.println(node.val);
} else {
System.out.println("Empty LinkedList");
}
}
}
25 changes: 25 additions & 0 deletions src/main/java/com/shuzijun/leetcode/Node.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.shuzijun.leetcode;

import java.util.List;

/**
* @author : CAOMU
* @version : 1.0
* @since : 2021/02/11, Thu, 1:21
*/
public class Node {
public int val;
public List<Node> children;

public Node() {
}

public Node(int _val) {
this.val = _val;
}

public Node(int _val, List<Node> _children) {
this.val = _val;
this.children = _children;
}
}
25 changes: 25 additions & 0 deletions src/main/java/com/shuzijun/leetcode/TreeNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.shuzijun.leetcode;

/**
* @author : CAOMU
* @version : 1.0
* @since : 2021/02/10, 水, 18:07
*/
public class TreeNode {
public TreeNode left;
public TreeNode right;
public int val;

TreeNode() {
}

public TreeNode(int val) {
this.val = val;
}

public TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
99 changes: 99 additions & 0 deletions src/main/java/com/shuzijun/leetcode/TreeNodeUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.shuzijun.leetcode;

import java.util.LinkedList;
import java.util.Queue;

/**
* @author : CAOMU
* @version : 1.0
* @since : 2021/02/19, Fri, 10:08
*/
public class TreeNodeUtils {
public static String treeNodeToString(TreeNode root) {
if (root == null) {
return "[]";
}

String output = "";
Queue<TreeNode> nodeQueue = new LinkedList<>();
nodeQueue.add(root);
while (!nodeQueue.isEmpty()) {
TreeNode node = nodeQueue.remove();

if (node == null) {
output += "null, ";
continue;
}

output += String.valueOf(node.val) + ", ";
nodeQueue.add(node.left);
nodeQueue.add(node.right);
}
return "[" + output.substring(0, output.length() - 2) + "]";
}

public static TreeNode stringToTreeNode(String input) {
input = input.trim();
input = input.substring(1, input.length() - 1);
if (input.length() == 0) {
return null;
}

String[] parts = input.split(",");
String item = parts[0];
TreeNode root = new TreeNode(Integer.parseInt(item));
Queue<TreeNode> nodeQueue = new LinkedList<>();
nodeQueue.add(root);

int index = 1;
while (!nodeQueue.isEmpty()) {
TreeNode node = nodeQueue.remove();

if (index == parts.length) {
break;
}

item = parts[index++];
item = item.trim();
if (!item.equals("null")) {
int leftNumber = Integer.parseInt(item);
node.left = new TreeNode(leftNumber);
nodeQueue.add(node.left);
}

if (index == parts.length) {
break;
}

item = parts[index++];
item = item.trim();
if (!item.equals("null")) {
int rightNumber = Integer.parseInt(item);
node.right = new TreeNode(rightNumber);
nodeQueue.add(node.right);
}
}
return root;
}

public static void prettyPrintTree(TreeNode node, String prefix, boolean isLeft) {
if (node == null) {
System.out.println("Empty tree");
return;
}

if (node.right != null) {
prettyPrintTree(node.right, prefix + (isLeft ? "│ " : " "), false);
}

System.out.println(prefix + (isLeft ? "└── " : "┌── ") + node.val);

if (node.left != null) {
prettyPrintTree(node.left, prefix + (isLeft ? " " : "│ "), true);
}
}

public static void prettyPrintTree(TreeNode node) {
prettyPrintTree(node, "", true);
}
}
64 changes: 64 additions & 0 deletions src/main/java/com/shuzijun/leetcode/Trie.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.shuzijun.leetcode;

/**
* @author : CAOMU
* @version : 1.0
* @since : 2021/02/16, Tue, 12:36
*/
public class Trie {
Trie[] children;
boolean isWord;

/**
* Initialize your data structure here.
*/
public Trie() {
this.children = new Trie[26];
this.isWord = false;
}

/**
* Inserts a word into the trie.
*/
public void insert(String word) {
this.insert(word.toCharArray());
}

public void insert(char[] chars) {
Trie curr = this;
for (int i = 0; i < chars.length; i++) {
char c = chars[i];
if (curr.children[c - 'a'] == null) {
curr.children[c - 'a'] = new Trie();
}
curr = curr.children[c - 'a'];
}
curr.isWord = true;
}

/**
* Returns if the word is in the trie.
*/
public boolean search(String word) {
Trie leaf = this.findLeafTrie(word);
return leaf != null && leaf.isWord;
}

/**
* Returns if there is any word in the trie that starts with the given prefix.
*/
public boolean startsWith(String prefix) {
return this.findLeafTrie(prefix) != null;
}

private Trie findLeafTrie(String word) {
Trie curr = this;
for (int i = 0; i < word.length(); i++) {
curr = curr.children[word.charAt(i) - 'a'];
if (curr == null) {
return null;
}
}
return curr;
}
}
57 changes: 57 additions & 0 deletions src/main/java/com/shuzijun/leetcode/UnionFind.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.shuzijun.leetcode;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.IntStream;

/**
* @author : CAOMU
* @version : 1.0
* @since : 2021/02/14, Sun, 14:56
*/
public class UnionFind {
private int[] roots;

protected UnionFind(int n) {
this.roots = IntStream.range(0, n).toArray();
}

protected int[] getRoots() {
return this.roots;
}

protected void union(int p, int q) {
this.roots[this.findRoot(p)] = this.findRoot(q);
}

protected void union(int... p) {
for (int i = 1; i < p.length; i++) {
this.union(p[i], p[0]);
}
}

protected int findRoot(int i) {
int root = i;
while (root != this.roots[root]) {
root = this.roots[root];
}
int t;
while (i != this.roots[root]) {
t = this.roots[i];
this.roots[i] = root;
i = t;
}
return root;
}

protected boolean isConnected(int p, int q) {
return this.findRoot(p) == this.findRoot(q);
}

protected int getCount() {
Set<Integer> rootSet = new HashSet<>();
Arrays.stream(this.roots).forEach(root -> rootSet.add(this.findRoot(root)));
return rootSet.size();
}
}
Loading