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;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值