Leetcode刷题工具类

我在leetcode刷题时,习惯在本地IDE上编码调试,经常需要自己生成输入的参数,如链表、二叉树,因此记录一下平时刷题常用到方法。

public class LeetCodeUtil {

    /**
     * 根据数组生成链表
     *
     * @param arr
     * @return
     */
    public static ListNode genListNode(int[] arr) {
        ListNode n = new ListNode(arr[0]);
        ListNode head = n;
        for (int i = 1; i < arr.length; i++) {
            ListNode node = new ListNode(arr[i]);
            n.next = node;
            n = node;
        }
        n.next = null;
        return head;
    }

    /**
     * 打印链表
     *
     * @param node
     */
    public static void printListNode(ListNode node) {
        while (node != null) {
            String result = node.next == null ? node.val + "" : node.val + ",";
            node = node.next;
            System.out.print(result);
        }
    }

    /**
     * 根据数组生成二叉树
     *
     * @param arr
     * @return
     */
    public static TreeNode genTreeNode(int[] arr) {
        return genTreeNode(arr, 0);
    }

    private static TreeNode genTreeNode(int[] arr, int index) {
        TreeNode node = null;
        if (index < arr.length) {
            node = new TreeNode(arr[index]);
            node.left = genTreeNode(arr, 2 * index + 1);
            node.right = genTreeNode(arr, 2 * index + 2);
        }
        return node;
    }

    /**
     * 按顺序打印二叉树
     *
     * @param root
     */
    public static void printTreeNode(TreeNode root) {
        List list = new ArrayList();
        preOrder(root, list);
        Collections.sort(list);
        System.out.println(list);
    }

    /**
     * 二叉树先序遍历(非递归)
     *
     * @param root
     */
    private static void preOrder(TreeNode root, List list) {
        Stack<TreeNode> stack = new Stack<>();
        while (true) {
            while (root != null) {
//                System.out.print(root.val + ",");
                list.add(root.val);
                stack.push(root);
                root = root.left;
            }
            if (stack.isEmpty()) break;
            root = stack.pop();
            root = root.right;
        }
    }

    /**
     * 打印数组
     *
     * @param arr
     */
    public static void printArr(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + (i == arr.length - 1 ? "" : ","));
        }
    }

    /**
     * 快排
     *
     * @param arr
     * @param leftIndex
     * @param rightIndex
     */
    public static void quickSort(int[] arr, int leftIndex, int rightIndex) {
        if (leftIndex >= rightIndex) {
            return;
        }
        int left = leftIndex;
        int right = rightIndex;
        //待排序的第一个元素作为基准值
        int key = arr[left];
        //从左右两边交替扫描,直到left = right
        while (left < right) {
            while (right > left && arr[right] >= key) {
                //从右往左扫描,找到第一个比基准值小的元素
                right--;
            }
            //找到这种元素将arr[right]放入arr[left]中
            arr[left] = arr[right];
            while (left < right && arr[left] <= key) {
                //从左往右扫描,找到第一个比基准值大的元素
                left++;
            }
            //找到这种元素将arr[left]放入arr[right]中
            arr[right] = arr[left];
        }
        //基准值归位
        arr[left] = key;
        //对基准值左边的元素进行递归排序
        quickSort(arr, leftIndex, left - 1);
        //对基准值右边的元素进行递归排序。
        quickSort(arr, right + 1, rightIndex);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值