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

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
逻辑结构:描述数据元素之间的逻辑关系,如线性结构(如数组、链表)、树形结构(如二叉树、堆、B树)、图结构(有向图、无向图等)以及集合和队列等抽象数据类型。 存储结构(物理结构):描述数据在计算机中如何具体存储。例如,数组的连续存储,链表的动态分配节点,树和图的邻接矩阵或邻接表表示等。 基本操作:针对每种数据结构,定义了一系列基本的操作,包括但不限于插入、删除、查找、更新、遍历等,并分析这些操作的时间复杂度和空间复杂度。 算法: 算法设计:研究如何将解决问题的步骤形式化为一系列指令,使得计算机可以执行以求解问题。 算法特性:包括输入输出、有穷性、确定性和可行性。即一个有效的算法必须能在有限步骤内结束,并且对于给定的输入产生唯一的确定输出。 算法分类:排序算法(如冒泡排序、快速排序、归并排序),查找算法(如顺序查找、二分查找、哈希查找),图论算法(如Dijkstra最短路径算法、Floyd-Warshall算法、Prim最小生成树算法),动态规划,贪心算法,回溯法,分支限界法等。 算法分析:通过数学方法分析算法的时间复杂度(运行时间随数据规模增长的速度)和空间复杂度(所需内存大小)来评估其效率。 学习算法与数据结构不仅有助于理解程序的内部工作原理,更能帮助开发人员编写出高效、稳定和易于维护的软件系统。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值