Java的ACM模式输入输出

1.输入为一个链表

示例:反转链表

输入是一串数字,请将其转换成单链表格式之后,再进行操作
        //输入描述:
        //一串数字,用逗号分隔
        //输入
        //1,2,3,4,5

        //输出
        //1,5,2,4,3

import java.util.*;

public class Main {
    public static class LinkedNode {
        int val;
        LinkedNode next;
        public LinkedNode(int val) {
            this.val = val;
        }
        public LinkedNode(int val, LinkedNode next) {
            this.val = val;
            this.next = next;
        }        
    }
    
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.next().toString();
        String[] strs = str.split(",");
        int[] arr = new int[strs.length];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = Integer.parseInt(strs[i]);
        }
        LinkedNode head = new LinkedNode(0);
        LinkedNode p = head;
        for (int a : arr) {
            p.next = new LinkedNode(a);
            p = p.next;
        }
        LinkedNode res = help(head.next);
        while (res != null) {
            if (res.next == null) {
                System.out.print(res.val);
            } else {
                System.out.print(res.val + ",");
            }
            res = res.next;
        }        
    }
    
    public static LinkedNode help(LinkedNode head) {
        LinkedNode pre = null;
        LinkedNode cur = head;
        while (cur != null) {
            LinkedNode temp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = temp;
        }
        return pre;     
    }
}

2.输入为二叉树

示例:层序遍历

输入:3,9,20,-1,-1,15,7

输出:[[3], [9, 20], [15, 7]]

import java.util.*;

public class Main {
    static List<List<Integer>> res = new LinkedList<>();
    private static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        public TreeNode() {}
        public TreeNode(int val) {
            this.val = val;
            this.left = null;
            this.right = null;
        }
    }
    
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.next();
        String[] splits = str.split(",");
        int[] arr = new int[splits.length];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = Integer.parseInt(splits[i]);
        }
        TreeNode root = build(arr);
        help(root);
        System.out.print(res);
//         for (List<Integer> list : res) {
//             System.out.print(list);
//         }
    }
    
    private static void help(TreeNode root) {
        dfs(root, 0);
    }
    
    private static void dfs(TreeNode root, int depth) {
        if (root == null) {
            return;
        }
        depth++;
        if (res.size() < depth) {
            res.add(new LinkedList<>());
        }
        res.get(depth - 1).add(root.val);
        dfs(root.left, depth);
        dfs(root.right, depth);
    }
    
    private static TreeNode build(int[] arr) {
        List<TreeNode> treeNodeList = arr.length > 0 ? new ArrayList<>(arr.length) : null;
        TreeNode root = null;
        for (int i = 0; i < arr.length; i++) {
            TreeNode node = null;
            if (arr[i] != -1) {
                node = new TreeNode(arr[i]);
            }
            treeNodeList.add(node);
            if (i == 0) {
                root = node;
            }
        }
        for (int i = 0; i * 2 + 2 < arr.length; i++) {
            TreeNode node = treeNodeList.get(i);
            if (arr[i] != -1) {
                node.left = treeNodeList.get(i * 2 + 1);
                node.right = treeNodeList.get(i * 2 + 2);
            }
        }
        return root;
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_43955488/article/details/125344861