Java之ACM模式


前言

力扣都是力扣模式,而面试笔试会有acm模式,:older_man:不会,寄了

一些方法

hasNextXXX

  • 会忽略空格

nextLine()

  • 读取一整行,将\n前的内容全部返回

next()

读取下一个字符串,以空格为分隔

牛客上的练习

属于都是比较简单的,没涉及什么树啊链表什么的

A-A+B(1)

import java.util.Scanner;
public class Main{
    public static void main(String[]args)
    {
        Scanner sc=new Scanner(System.in);
        while(sc.hasNextInt())
        {
            int a=sc.nextInt();
            int b=sc.nextInt();
            System.out.println(a+b);
        }
    }
}

B-A+B(2)

import java.util.Scanner;
public class Main
{
    public static void main(String []args)
    {
        Scanner sc=new Scanner(System.in);
        int count=sc.nextInt();
        while(count>0)
        {
            int a=sc.nextInt();
            int b=sc.nextInt();
            System.out.println(a+b);
            count--;
        }
    }
}

C-A+B(3)

这题用nextLine()试试

import java.util.Scanner;
public class Main
{
    public static void main(String []args)
    {
        Scanner sc=new Scanner(System.in);
        while(sc.hasNextInt())
        {
            String[]nums=sc.nextLine().split(" ");
            int a=Integer.parseInt(nums[0]);
            int b=Integer.parseInt(nums[1]);
            if(a==0||b==0)
            {
                break;
            }
            System.out.println(a+b);
        }
    }
}

D-A+B(4)

import java.util.Scanner;
public class Main {
    public static void main(String[]args)
    {
        Scanner sc=new Scanner(System.in);
        while(sc.hasNextInt())
        {
            String[]nums=sc.nextLine().split(" ");
            if(nums[0].equals("0"))
            {
                break;
            }
            int sum=0;
            for(int i=1;i<nums.length;i++)
            {
                sum+=Integer.parseInt(nums[i]);
            }
            System.out.println(sum);
        }
    }
}

E-A+B(5)

import java.util.Scanner;
public class Main {
    public static void main(String []args)
    {
        Scanner sc=new Scanner(System.in);
        int count=Integer.parseInt(sc.nextLine().split(" ")[0]);
        while(count>0)
        {
            String[]nums=sc.nextLine().split(" ");
            int sum=0;
            for(int i=1;i<nums.length;i++)
            {
                sum+=Integer.parseInt(nums[i]);
            }
            System.out.println(sum);
            count--;
        }
    }
} 

一开始:

Scanner sc=new Scanner(System.in);
        int count=sc.nextInt();
        while(count>0)

这就错了,因为nextInt()不会读取换行符

F-A+B(6)

import java.util.Scanner;
public class Main {
    public static void main(String []args)
    {
        Scanner sc=new Scanner(System.in);
        while(sc.hasNextLine())
        {
            String[]nums=sc.nextLine().split(" ");
            int sum=0;
            for(int i=1;i<nums.length;i++)
            {
                sum+=Integer.parseInt(nums[i]);
            }
            System.out.println(sum);
        }
    }
}

H-字符串排序(1)

注意:Arrays在java.util.Arrays包内

import java.util.Scanner;
import java.util.Arrays;
public class Main
{
    public static void main(String[]args)
    {
        Scanner sc=new Scanner(System.in);
        sc.nextLine();
        String []strs=sc.nextLine().split(" ");
        Arrays.sort(strs);
        for(String s:strs)
        {
            System.out.print(s+" ");
        }
    }
}

I-字符串排序(2)

import java.util.Scanner;
import java.util.Arrays;
public class Main
{
    public static void main(String []args)
    {
        Scanner sc=new Scanner(System.in);
        while(sc.hasNextLine())
        {
            String[]strs=sc.nextLine().split(" ");
            Arrays.sort(strs);
            for(String s:strs)
            {
                System.out.print(s+" ");
            }
            System.out.println();
        }
    }
}

树和链表

自己构造链表

以单链表反转为例子

注意事项

  • 类都写在main类里
  • 解决问题的方法单独一个函数
  • 然后调用这个函数完输出就行了
import java.util.Scanner  ;
public class Main
{
    public static class Node
    {
        int val;
        Node next;
        public Node(){}
        public Node(int val)
        {
            this.val=val;
        }
    }
    
    public static void print(Node list)
    {
        StringBuilder sb=new StringBuilder();
        while(list!=null)
        {
            sb.append(list.val).append(" ");
            list=list.next;
        }
        System.out.println(sb.toString().trim());
    }
    
    public static Node reverseList(Node list)
    {
        Node pre=null;
        Node cur=list;
        while(cur!=null)
        {
            Node aft=cur.next;
            cur.next=pre;
            pre=cur;
            cur=aft;
        }
        return pre;
    }
    public static void main(String []args)
    {
        Scanner sc=new Scanner(System.in);
        while(sc.hasNextInt())
        {
            int n = sc.nextInt();
            if (n == 0) {
                System.out.println("list is empty");
                continue;
            }
            Node list=new Node(sc.nextInt());
            Node p=list;
            for(int i=1;i<n;i++)
            {
                p.next=new Node(sc.nextInt());
                p=p.next;
            }
            print(list);
            list=reverseList(list);
            print(list);
        }
        sc.close();
    }
}

自己构造树

题目页面 (kamacoder.com)

出生题目啊!



import java.util.HashMap;
import java.util.Scanner;

// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
// then press Enter. You can now see whitespace characters in your code.
public class Main {
    private static HashMap<Character,Integer>mp;
    private static int nodeIndex;
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        while(sc.hasNextLine())
        {
            String[]strs=sc.nextLine().split(" ");
            char[]preorder=strs[0].toCharArray();
            char[]inorder=strs[1].toCharArray();
            mp=new HashMap<>();
            nodeIndex=0;
            for(int i=0;i<preorder.length;i++)
            {
                mp.put(inorder[i],i);
            }
            TreeNode root=buildTree(preorder,inorder);
            dfs(root);
            System.out.println("");
        }
    }

    public static  class TreeNode
    {
        char val;
        TreeNode left;
        TreeNode right;
        TreeNode(){}
        TreeNode(char val)
        {
            this.val=val;
        }
    }
    private static TreeNode buildTree(char[]preorder,char[]inorder)
    {
        int n=preorder.length;
        return build(preorder,inorder,0,n-1);
    }
    private static TreeNode build(char[]preorder,char[]inorder,int l,int r)
    {
        int n=preorder.length;
        if(nodeIndex>=n||l>r)return null;
        char rootVal=preorder[nodeIndex];
        TreeNode root=new TreeNode(rootVal);
        ++nodeIndex;
        int mid=mp.get(rootVal);
        root.left=build(preorder,inorder,l,mid-1);
        root.right=build(preorder,inorder,mid+1,r);
        return  root;
    }
    private static void dfs(TreeNode node)
    {
        if(node==null)return;
        dfs(node.left);
        dfs(node.right);
        System.out.print(node.val);
    }


}

题目页面 (kamacoder.com)

纯纯出生题目我操


import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;
class TreeNode
{
    int val;//当前位置
    int letter;//字母标识
    TreeNode left;//左右节点
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val,int letter) {
        this.val = val;
        this.letter = letter;
    }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}
public class Main {

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();//node个数
        sc.nextLine();
        HashMap<Integer,TreeNode>mp=new HashMap<>();
        String[][]str=new String[n+1][3];
        boolean []isRoot=new boolean[n+1];//是否是root
        Arrays.fill(isRoot,true);
        for(int i=1;i<=n;i++)
        {
            str[i]=sc.nextLine().split(" ");//按行分割放到矩阵里
            TreeNode node = new TreeNode(i, (int)str[i][0].charAt(0) - 'A');
            mp.put(i,node);//第一轮将val和node的对应关系放到map里
        }

        TreeNode root=null;
        for(int i=1;i<=n;i++)
        {
            TreeNode node=mp.get(i);
            if(str[i][1].equals("0"))
            {
                node.left=null;
            }else
            {
                int left=Integer.parseInt(str[i][1]);
                node.left=mp.get(left);
                isRoot[left]=false;
            }
            if(str[i][2].equals("0"))
            {
                node.right=null;
            }else
            {
                int right=Integer.parseInt(str[i][2]);
                node.right=mp.get(right);
                isRoot[right]=false;
            }
        }
        for(int i=1;i<=n;i++)
        {
            if(isRoot[i]==true)
            {
                root=mp.get(i);
                break;
            }
        }
        preOrder(root);
        System.out.println();
        inOrder(root);
        System.out.println();
        postOrder(root);
        System.out.println();
    }
    public static void preOrder(TreeNode root){//前序遍历
        if(root != null){
            System.out.print((char)(root.letter + 'A'));
            preOrder(root.left);
            preOrder(root.right);
        }
    }

    public static void inOrder(TreeNode root){//中序遍历
        if(root != null){
            inOrder(root.left);
            System.out.print((char)(root.letter + 'A'));
            inOrder(root.right);
        }
    }

    public static void postOrder(TreeNode root){//后序遍历
        if(root != null){
            postOrder(root.left);
            postOrder(root.right);
            System.out.print((char)(root.letter + 'A'));
        }
    }
}

  目录