Home
  • 计算机网络
  • 操作系统
  • 数据结构与算法
  • 设计模式
  • JavaSE
  • JVM
  • JUC
  • Netty
  • CPP
  • QT
  • UE
  • Go
  • Gin
  • Gorm
  • HTML
  • CSS
  • JavaScript
  • vue2
  • TypeScript
  • vue3
  • react
  • Spring
  • SpringMVC
  • Mybatis
  • SpringBoot
  • SpringSecurity
  • SpringCloud
  • Mysql
  • Redis
  • 消息中间件
  • RPC
  • 分布式锁
  • 分布式事务
  • 个人博客
  • 弹幕视频平台
  • API网关
  • 售票系统
  • 消息推送平台
  • SaaS短链接系统
  • Linux
  • Docker
  • Git
GitHub (opens new window)
Home
  • 计算机网络
  • 操作系统
  • 数据结构与算法
  • 设计模式
  • JavaSE
  • JVM
  • JUC
  • Netty
  • CPP
  • QT
  • UE
  • Go
  • Gin
  • Gorm
  • HTML
  • CSS
  • JavaScript
  • vue2
  • TypeScript
  • vue3
  • react
  • Spring
  • SpringMVC
  • Mybatis
  • SpringBoot
  • SpringSecurity
  • SpringCloud
  • Mysql
  • Redis
  • 消息中间件
  • RPC
  • 分布式锁
  • 分布式事务
  • 个人博客
  • 弹幕视频平台
  • API网关
  • 售票系统
  • 消息推送平台
  • SaaS短链接系统
  • Linux
  • Docker
  • Git
GitHub (opens new window)
  • 哈希表

    • 缓存旧值
    • 缓存索引
    • 前缀和
    • 原地哈希
    • 等价代换
    • 哈希结构
  • 双指针

  • 数组

  • 字符串

  • 链表

  • 树

  • 回溯

  • 动态规划

  • 图

  • 二分查找

  • 贪心

  • 栈&队列

  • 堆

  • 位运算

  • 数据结构设计

  • rui的精选题单

  • 笔试真题

  • LeetCode周赛

  • ACM模式输入输出
    • Scanner常用API
    • 二维数组
    • 二维字符数组
    • 链表输入
    • 二叉树构建
  • 数学

  • 数据结构与算法
Nreal
2024-03-08
目录

ACM模式输入输出

# Scanner常用API

  • hasNextInt():当下一个字符是整数时返回 true, 反之返回 false;

    nextInt():把字符转换为int类型并返回;

  • hasNextLine():当下一位还有字符时返回 true, 反之返回 false;

    nextLine():把本行剩下的字符转化位String类型并返回;

  • hasNext():当下一位还有字符时返回true,反之返回false;

    next():把两个空格之间的字符转换为String类型并返回;

# 二维数组

3 2

1 2 3

4 5 6

main(){
    Scanner sc = new Scanner(System.in);
    int m = sc.nextInt();
    int n = sc.nextInt();
    int[][] arr = new int[m][n];
    scc.nextLine(); // 跳过行列后的回车符
    for(int i=0;i<m;i++){
        for(int j=0;j<n;j++){
            arr[i][j] = sc.nextInt();
            
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13

# 二维字符数组

# 链表输入

翻转链表;

public class ListInput {
    //先构建一个节点类,用于链表构建
    static class ListNode {
        int val;
        ListNode next;
        public ListNode(int val){
            this.val = val;
        }
    }
	public static void main(String[] args){
        //输入是一串数字,请将其转换成单链表格式之后,再进行操作
        //输入描述:
        //一串数字,用逗号分隔
        //输入
        //1,2,3,4,5
        Scanner sc = new Scanner(System.in);
        //以字符串形式作为输入
        String str = sc.next().toString();
        //通过分隔符将其转为字符串数组
        String[] ss  = str.split(",");
		//初始化一个整数数组
        int[] nums = new int[ss.length];
        //给整数数组赋值
        for(int j = 0; j<nums.length;j++) {
            nums[j] = Integer.parseInt(ss[j]);
        }
        Stack<ListNode> stack = new Stack<>();
        ListNode head = new ListNode(0);
        ListNode p = head;
        //链表初始化并放入stack中
        for(int i=0;i<nums.length;i++){
            p.next = new ListNode(nums[i]);
            p = p.next;
            stack.add(p);
        }
        p = head;
        while(!stack.isEmpty()){
            q = stack.pop();
            p.next = q;
            p = q;
        }
        head = head.next;
        while(head!=null){
            if(head.next == null){
                System.out.print(head.val);
            }else{
                System.out.print(head.val + ",");
            }
            head = head.next;
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

# 二叉树构建

public class BT {

    public static class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;
        public TreeNode(int val) {
            this.val = val;
        }
        public TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
    public static TreeNode construct(int[] arr){
        List<TreeNode> list = new ArrayList<>();
        TreeNode root = null;
        int n = arr.length;
        for(int i=0;i<n;i++){
            TreeNode node = new TreeNode(arr[i]);
            list.add(node);
            if(i==0)
                root = node; // 构建根节点
        }
        for(int i=0;i*2+1<n;i++){
            TreeNode node = list.get(i);
            if(node!=null){
                node.left = list.get(i*2+1);
                if(i*2+2<n){
                    node.right = list.get(i*2+2);
                }
            }
        }
        return root;
    }

    // 二叉树右视图
    public static void main(String[] args) {
        int[] arr = {1,2,3,-1,5,-1,4};
        TreeNode root = construct(arr);
        List<Integer> res = new ArrayList<>();
        Queue<TreeNode> que = new LinkedList<>();
        que.offer(root);
        while(!que.isEmpty()){
            int size = que.size();
            while(size>0){
                TreeNode node = que.poll();
                if(size==1)
                    res.add(node.val);
                if(node.left!=null)
                    que.add(node.left);
                if(node.right!=null)
                    que.add(node.right);
                size--;
            }
        }
        for(int i=0;i<res.size();i++){
            System.out.println(res.get(i));
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
第382场周赛
质数

← 第382场周赛 质数→

Theme by Vdoing | Copyright © 2021-2024
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式