java 刷题自己写的工具类

刷题的时候每次都要做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<Integer> printLevelBinaryTree(TreeNode root){
		List<Integer> list = new ArrayList<Integer>();
		if(root == null){
			return list;
		}
		Queue<TreeNode> queue = new LinkedList<TreeNode>();
		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<TreeNode> 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;
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值