javaブラシは自分の書くツール種類を書きます。
4257 ワード
問題を書く時は毎回Caseを作りますが、毎日チェーンを作るテスト用の例を書くと崩壊します。車輪を繰り返したくないので、自分でUtil類をカプセル化して、問題を書く時のテストツールに使います。
もちろん今後、ブログで書いた問題もテスト用の例を公開します。
もちろん今後、ブログで書いた問題もテスト用の例を公開します。
package com.leetcode.LinkedList;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class Util {
public static void printArray(int[] data){
for(int obj:data){
print(obj+",");
}
println();
}
/**
* ,(BFS)
* @param root
* @return
*/
public static List printLevelBinaryTree(TreeNode root){
List list = new ArrayList();
if(root == null){
return list;
}
Queue queue = new LinkedList();
queue.add(root);
TreeNode front = null;
while(!queue.isEmpty()){
front = queue.poll();
list.add(front.val);
print(""+front.val+",");
if(front.left!=null){
queue.add(front.left);
}
if(front.right != null){
queue.add(front.right);
}
}
return list;
}
/***
* ,
* @param str:
* @return
*/
public static TreeNode creatBinaryTree(String str) {
if (str == null && str.equals("")) {
return null;
}
TreeNode root = null;
String[] strs = str.split(",");
Queue queue = new LinkedList<>();
if(strs[0].equals("null")) return null;
root = new TreeNode(Integer.parseInt(strs[0]));
queue.add(root);
TreeNode front = null;
for (int i = 1; i < strs.length; i++) {
if( (i & 0x1) != 0){
front = queue.poll();
}
// println("front:"+front.val);
if ((i & 0x1) != 0) {// odd
if (strs[i].equals("null")) {
front.left = null;
} else {
front.left = new TreeNode(Integer.parseInt(strs[i]));
queue.add(front.left);
}
} else {// odd
if (strs[i].equals("null")) {
front.right = null;
} else {
front.right = new TreeNode(Integer.parseInt(strs[i]));
queue.add(front.right);
}
}
}
return root;
}
/**
*
*
* @param head
*/
public static void printList(ListNode head) {
ListNode temp = head;
while (temp != null) {
System.out.print(temp.val + ",");
temp = temp.next;
}
System.out.println();
}
/**
* ( ),
*
* @param root
*/
public static void BinaryTreePreOrder(TreeNode root) {
if (root != null) {
print(""+root.val+",");
BinaryTreePreOrder(root.left);
BinaryTreePreOrder(root.right);
}
}
/**
* ( ),
*
* @param root
*/
public static void BinaryTreeInOrder(TreeNode root) {
if (root != null) {
BinaryTreeInOrder(root.left);
print(root.val + ",");
BinaryTreeInOrder(root.right);
}
}
/**
* ( ),
*
* @param root
*/
public static void BinaryTreePostOrder(TreeNode root) {
if (root != null) {
BinaryTreePostOrder(root.left);
BinaryTreePostOrder(root.right);
print(root.val + ",");
}
}
/**
* ,
*
* @param obj
*/
public static void print(Object obj) {
System.out.print(obj);
}
public static void println(){
System.out.println();
}
/**
* ,
*
* @param obj
*/
public static void println(Object obj) {
System.out.println(obj);
}
/***
*
* @param obj
*/
public static void println(int[] obj) {
for(int i:obj){
print(i+",");
}
println();
}
public void println(char[] str){
for(char c:str){
print(c+",");
}
println();
}
/**
* ,
*
* @param str:
* ,
* @return
*/
public static ListNode creatList(String str) {
if (null == str || str.equals("")) {
try {
// throw new Exception("str==null");
} catch (Exception e) {
e.printStackTrace();
} finally {
return null;
}
}
String[] strs = str.split(",");
ListNode head = new ListNode(Integer.parseInt(strs[0]));
ListNode curr = head;
ListNode post = null;
for (int i = 1; i < strs.length; i++) {
post = new ListNode(Integer.parseInt(strs[i]));
curr.next = post;
curr = curr.next;
}
return head;
}
}