【JAVA】acwing语法基础课刷题(138 / 138)完结

目录

一、输入输出

1、低效率 - 输入输出

2、高效率 - 输入输出 

1、输入规模较大时使用

2、输出规模较大时使用

二、判断语句和循环语句

!简单斐波那契

!试除法求约数

!试除法判断约数726. 质数 - AcWing题库

 !菱形 - 困难

三、数组

!回字形方阵

!斜对角线矩阵754. 平方矩阵 II - AcWing题库

!蛇形矩阵

四、字符串

!双指针算法 - 字符串中最长的连续出现的字符771. 字符串中最长的连续出现的字符 - AcWing题库

!字符串循环移位包含问题

!字符串的乘方 

!indexOf和lastIndexOf的应用

五、函数

欧几里得求最大公约数gcd

!821.跳台阶 - dp / 斐波那契数列

!822.走方格 - dp 

!823.排列 - dfs

六、类与接口

! 斐波那契三个版本写法

1、dp版

2、轮转数组

3、递归版

!在O(1)时间删除链表节点

!35.反转链表

!66.两个链表的第一个公共节点

!29.删除链表重复节点

 !类似于c++结构体排序 862.三元组排序

七、常用容器

1、栈

2、Map

3、队列 + 优先队列 PriorityQueue

4、Set

lowbit()


一、输入输出

1、低效率 - 输入输出

import java.util.Scanner; //导入包

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);//scanner实例化
        
        String name=sc.next(); //string型输入
        int age=sc.nextInt();  //int型输入
        double weight=sc.nextDouble();  //double型输入
        boolean f=sc.nextBoolean();  //boolean型输入

        //对于char型输入,scanner没有提供相关方法
        String gender=sc.next();
        char c=gender.charAt(0);//获取索引为0位置上的字符
        System.out.println(c);
        System.out.printf("%04d %.2f\n", 4, 123.456D);  // 格式化输出,float与double都用%f输出
    }
}

2、高效率 - 输入输出 

1、输入规模较大时使用

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main
{
    public static void main(String[] args) throws Exception
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String str=br.readLine();
        char c=br.read();
        System.out.println(str);
    }
}

2、输出规模较大时使用

import java.io.BufferedWriter;
import java.io.OutputStreamWriter;

public class Main {
    public static void main(String[] args) throws Exception {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        bw.write("Hello World\n");
        bw.flush();  // 需要手动刷新缓冲区
    }
}

 1. A + B - AcWing题库

import java.util.Scanner; //导入包
/*
   一个java源文件可以声明多个class 但最多有一个类声明为public
   要求声明为public的类的类名必须与源文件相同
*/

public class Main
{
    public static void main(String[] args)//程序的入口 格式固定
    {
        Scanner sc=new Scanner(System.in);//scanner实例化
        int x=sc.nextInt(),y=sc.nextInt();//输入int类型
        System.out.println(x+y);
        //System.out.print() 不换行
        //System.out.println() 换行
        //System.out.printf("%.2f\n",num);
    }
}

608. 差 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int a=sc.nextInt(),b=sc.nextInt();
        int c=sc.nextInt(),d=sc.nextInt();
        System.out.println("DIFERENCA = "+(a*b-c*d));
    }
}

604. 圆的面积 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        double r=sc.nextDouble();
        System.out.printf("A=%.4f",r*r*3.14159);
    }
}

606. 平均数1 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        double a=sc.nextDouble(),b=sc.nextDouble();
        System.out.printf("MEDIA = %.5f",(a*3.5+b*7.5)/11);
    }
}

609. 工资 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int num=sc.nextInt(),t=sc.nextInt();
        double q=sc.nextDouble();
        System.out.println("NUMBER = "+num);
        System.out.printf("SALARY = U$ %.2f\n",q*t);
    }
}

615. 油耗 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int km=sc.nextInt();
        double x=sc.nextDouble();
        System.out.printf("%.3f km/l",km/x);
    }
}

616. 两点间的距离 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        double x1=sc.nextDouble(),y1=sc.nextDouble();
        double x2=sc.nextDouble(),y2=sc.nextDouble();
        double dx=x2-x1,dy=y2-y1;
        System.out.printf("%.4f",Math.sqrt(dx*dx+dy*dy));
    }
}

653. 钞票 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int m=sc.nextInt();
        System.out.println(m);
        System.out.println(m/100+" nota(s) de R$ 100,00");
        m%=100;
        System.out.println(m/50+" nota(s) de R$ 50,00");
        m%=50;
        System.out.println(m/20+" nota(s) de R$ 20,00");
        m%=20;
        System.out.println(m/10+" nota(s) de R$ 10,00");
        m%=10;
        System.out.println(m/5+" nota(s) de R$ 5,00");
        m%=5;
        System.out.println(m/2+" nota(s) de R$ 2,00");
        m%=2;
        System.out.println(m+" nota(s) de R$ 1,00");
    }
}

654. 时间转换 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        int h=t/3600;
        t%=3600;
        int m=t/60;
        t%=60;
        System.out.printf("%d:%d:%d",h,m,t);
    }
}

605. 简单乘积 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int a=sc.nextInt(),b=sc.nextInt();
        System.out.println("PROD = "+a*b);
    }
}

611. 简单计算 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        sc.nextInt();
        int s1=sc.nextInt();
        double p1=sc.nextDouble();
        sc.nextInt();
        int s2=sc.nextInt();
        double p2=sc.nextDouble();
        System.out.printf("VALOR A PAGAR: R$ %.2f",s1*p1+s2*p2);
    }
}

612. 球的体积 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int r=sc.nextInt();
        double v=(4/3.0)*3.14159*r*r*r;
        System.out.printf("VOLUME = %.3f",v);
    }
}

613. 面积 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        double a=sc.nextDouble(),b=sc.nextDouble(),c=sc.nextDouble();
        System.out.printf("TRIANGULO: %.3f\n",a*c/2);
        System.out.printf("CIRCULO: %.3f\n",3.14159*c*c);
        System.out.printf("TRAPEZIO: %.3f\n",(a+b)*c/2);
        System.out.printf("QUADRADO: %.3f\n",b*b);
        System.out.printf("RETANGULO: %.3f\n",a*b);
    }
}

607. 平均数2 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        double a=sc.nextDouble(),b=sc.nextDouble(),c=sc.nextDouble();
        System.out.printf("MEDIA = %.1f",(a*2+b*3+c*5)/10);
    }
}

610. 工资和奖金 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        sc.next();
        double m=sc.nextDouble(),n=sc.nextDouble();
        System.out.printf("TOTAL = R$ %.2f",m+n*0.15);
    }
}

614. 最大值 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int a=sc.nextInt(),b=sc.nextInt(),c=sc.nextInt();
        System.out.println(Math.max(a,Math.max(b,c))+" eh o maior");
    }
}

617. 距离 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int x=sc.nextInt();
        System.out.println(x*2+" minutos");
    }
}

618. 燃料消耗 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        double v=sc.nextInt(),t=sc.nextInt();
        double s=v*t/12;
        System.out.printf("%.3f",s);
    }
}

656. 钞票和硬币 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);

        double x = sc.nextDouble();
        System.out.println("NOTAS:");
        System.out.printf("%d nota(s) de R$ 100.00\n", (int)(x / 100.0));
        x %= 100;
        System.out.printf("%d nota(s) de R$ 50.00\n", (int)(x / 50.0));
        x %= 50;
        System.out.printf("%d nota(s) de R$ 20.00\n", (int)(x / 20.0));
        x %= 20;
        System.out.printf("%d nota(s) de R$ 10.00\n", (int)(x / 10.0));
        x %= 10;
        System.out.printf("%d nota(s) de R$ 5.00\n", (int)(x / 5.0));
        x %= 5;
        System.out.printf("%d nota(s) de R$ 2.00\n", (int)(x / 2.0));
        x %= 2;
        System.out.println("MOEDAS:");
        System.out.printf("%d moeda(s) de R$ 1.00\n", (int)(x / 1.0));
        x %= 1.0;
        System.out.printf("%d moeda(s) de R$ 0.50\n", (int)(x / 0.5));
        x %= 0.5;
        System.out.printf("%d moeda(s) de R$ 0.25\n", (int)(x / 0.25));
        x %= 0.25;
        System.out.printf("%d moeda(s) de R$ 0.10\n", (int)(x / 0.10));
        x %= 0.10;
        System.out.printf("%d moeda(s) de R$ 0.05\n", (int)(x / 0.05));
        x %= 0.05;
        System.out.printf("%d moeda(s) de R$ 0.01\n", (int)(x / 0.009));
    }
}

655. 天数转换 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int x=sc.nextInt();
        System.out.println(x/365+" ano(s)");
        x%=365;
        System.out.println(x/30+" mes(es)");
        x%=30;
        System.out.println(x+" dia(s)");
    }
}

二、判断语句和循环语句

if()  while()里边需要值为boolean型

像c++里的while(n--)  在java里应该是while(n-->0)

665. 倍数 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int a=sc.nextInt(),b=sc.nextInt();
        if(a%b==0||b%a==0) System.out.println("Sao Multiplos");
        else System.out.println("Nao sao Multiplos");
    }
}

660. 零食 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt(),num=sc.nextInt();
        double m;
        if(n==1) m=num*4;
        else if(n==2) m=num*4.5;
        else if(n==3) m=num*5;
        else if(n==4) m=num*2;
        else m=num*1.5;
        System.out.printf("Total: R$ %.2f",m);
    }
}

659. 区间 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        double x=sc.nextDouble();
        String s;
        if(x>=0&&x<=25) s="[0,25]";
        else if(x>25&&x<=50) s="(25,50]";
        else if(x>50&&x<=75) s="(50,75]";
        else if(x>75&&x<=100) s="(75,100]";
        else 
        {
            System.out.printf("Fora de intervalo");
            return;
        }
        System.out.printf("Intervalo "+s);
    }
}

664. 三角形 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        double a=sc.nextDouble(),b=sc.nextDouble(),c=sc.nextDouble();
        if(a+b<=c||a+c<=b||b+c<=a) System.out.printf("Area = %.1f",(a+b)*c/2);
        else System.out.printf("Perimetro = %.1f",a+b+c);
    }
}

667. 游戏时间 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int a=sc.nextInt(),b=sc.nextInt();
        if(a>b) System.out.printf("O JOGO DUROU %d HORA(S)",24-a+b);
        else if(a<b) System.out.printf("O JOGO DUROU %d HORA(S)",b-a);
        else System.out.printf("O JOGO DUROU 24 HORA(S)");
    }
}

669. 加薪 - AcWing题库

java不能用逗号在一行分隔语句

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        double x,y;
        int z;
        double sa=sc.nextDouble();
        if(sa<=400) {x=sa*1.15;y=sa*0.15;z=15;}
        else if(sa<=800) {x=sa*1.12;y=sa*0.12;z=12;}
        else if(sa<=1200) {x=sa*1.1;y=sa*0.1;z=10;}
        else if(sa<=2000) {x=sa*1.07;y=sa*0.07;z=7;}
        else {x=sa*1.04;y=sa*0.04;z=4;}
        System.out.printf("Novo salario: %.2f\n",x);
        System.out.printf("Reajuste ganho: %.2f\n",y);
        System.out.printf("Em percentual: %d %%\n",z);
    }
}

670. 动物 - AcWing题库

字符串 a.equals(b) 意思是字符串a=b

如果a是空字符串 则系统会报错

import java.util.Scanner;

public class Main {
    public static void main(String[] args) 
    {
        Scanner sc=new Scanner(System.in);
        String a=sc.next(),b=sc.next(),c=sc.next();

        String res;
        if ("vertebrado".equals(a)) 
        {
            if ("ave".equals(b)) {
                if ("carnivoro".equals(c)) {
                    res = "aguia";
                } else {
                    res = "pomba";
                }
            } else {
                if ("onivoro".equals(c)) {
                    res = "homem";
                } else {
                    res = "vaca";
                }
            }
        } else {
            if ("inseto".equals(b)) {
                if ("hematofago".equals(c)) {
                    res = "pulga";
                } else {
                    res = "lagarta";
                }
            } else {
                if ("hematofago".equals(c)) {
                    res = "sanguessuga";
                } else {
                    res = "minhoca";
                }
            }
        }
        System.out.println(res);
    }
}

657. 选择练习1 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int a=sc.nextInt(),b=sc.nextInt(),c=sc.nextInt(),d=sc.nextInt();
        if(b>c&&d>a&&c+d>a+b&&c>0&&d>0&&a%2==0) System.out.println("Valores aceitos");
        else System.out.println("Valores nao aceitos");
    }
}

671. DDD - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int x=sc.nextInt();
        String s;
        if(x==61) s="Brasilia";
        else if(x==71) s="Salvador";
        else if(x==11) s="Sao Paulo";
        else if(x==21) s="Rio de Janeiro";
        else if(x==32) s="Juiz de Fora";
        else if(x==19) s="Campinas";
        else if(x==27) s="Vitoria";
        else if(x==31) s="Belo Horizonte";
        else s="DDD nao cadastrado";
        System.out.println(s);
    }
}

662. 点的坐标 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        double x=sc.nextDouble(),y=sc.nextDouble();
        String s;
        if(x==0&&y==0) s="Origem";
        else if(x==0) s="Eixo Y";
        else if(y==0) s="Eixo X";
        else if(x>0&&y>0) s="Q1";
        else if(x<0&&y>0) s="Q2";
        else if(x<0&&y<0) s="Q3";
        else s="Q4";
        System.out.println(s);
    }
}

666. 三角形类型 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        double a=sc.nextDouble(),b=sc.nextDouble(),c=sc.nextDouble();
        String s;
        if(a>=b+c||b>=a+c||c>=b+a) System.out.println("NAO FORMA TRIANGULO");
        else
        {
            if(a*a==b*b+c*c||b*b==a*a+c*c||c*c==a*a+b*b) System.out.println("TRIANGULO RETANGULO");
            else if(a*a>b*b+c*c||b*b>a*a+c*c||c*c>b*b+a*a) System.out.println("TRIANGULO OBTUSANGULO");
            else if(a*a<b*b+c*c||b*b<a*a+c*c||c*c<b*b+a*a) System.out.println("TRIANGULO ACUTANGULO");
            if(a==b&&b==c) System.out.println("TRIANGULO EQUILATERO");
            else if(a==b||b==c||a==c) System.out.println("TRIANGULO ISOSCELES");
        }
    }
}

668. 游戏时间2 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int a=sc.nextInt(),b=sc.nextInt(),c=sc.nextInt(),d=sc.nextInt();
        int m=a*60+b,n=c*60+d;
        int t;
        if(m<n) t=n-m;
        else t=1440-m+n;
        
        int h=t/60;
        int s=t%60;
        System.out.printf("O JOGO DUROU %d HORA(S) E %d MINUTO(S)",h,s);
    }
}

672. 税 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        double x=sc.nextDouble();
        if(x<=2000) System.out.printf("Isento");
        else if(x<=3000) System.out.printf("R$ %.2f",(x-2000)*0.08);
        else if(x<=4500) System.out.printf("R$ %.2f",80+(x-3000)*0.18);
        else System.out.printf("R$ %.2f",350+(x-4500)*0.28);
    }
}

663. 简单排序 - AcWing题库

import java.util.*;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int a[]=new int[3];
        for(int i=0;i<3;i++) a[i]=sc.nextInt();
        int t[]=Arrays.copyOfRange(a,0,3);//复制数组
        Arrays.sort(t);
        for(int i=0;i<3;i++) System.out.println(t[i]);
        System.out.println("");
        for(int i=0;i<3;i++) System.out.println(a[i]);
    }
}

658. 一元二次方程公式 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        double a=sc.nextDouble(),b=sc.nextDouble(),c=sc.nextDouble();
        if(b*b-4*a*c<0||a==0) System.out.println("Impossivel calcular");
        else
        {
            double r1,r2;
            r1=(-b+Math.sqrt(b*b-4*a*c))/(2*a);
            r2=(-b-Math.sqrt(b*b-4*a*c))/(2*a);
            System.out.printf("R1 = %.5f\n",r1);
            System.out.printf("R2 = %.5f",r2);
        }
    }
}

661. 平均数3 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        double n1=sc.nextDouble(),n2=sc.nextDouble(),n3=sc.nextDouble(),n4=sc.nextDouble();
        double ave=(n1*2+n2*3+n3*4+n4)/10;
        System.out.printf("Media: %.1f\n",ave);
        if(ave>=7) System.out.printf("Aluno aprovado.");
        else if(ave<5) System.out.printf("Aluno reprovado.");
        else
        {
            System.out.printf("Aluno em exame.\n");
            double x=sc.nextDouble();
            System.out.printf("Nota do exame: %.1f\n",x);
            double t=(x+ave)/2;
            if(t>=5) System.out.printf("Aluno aprovado.\n");
            else System.out.printf("Aluno reprovado.\n");
            System.out.printf("Media final: %.1f",t);
            
        }
    }
}

714. 连续奇数的和 1 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int s=0;
        int a=sc.nextInt(),b=sc.nextInt();
        if(a>b) 
        {
            int t=a;
            a=b;
            b=t;
        }
        for(int i=a+1;i<b;i++) 
            if((i&1)==1) s+=i;//负数取余 eg:-7%5=-2 所以用%2==1会有错
        System.out.print(s);
    }
}

716. 最大数和它的位置 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int max=0,idx=1;
        for(int i=1;i<=100;i++)
        {
            int x=sc.nextInt();
            if(x>max) 
            {
                max=x;
                idx=i;
            }
        }
        System.out.println(max);
        System.out.println(idx);
    }
}

721. 递增序列 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        while(true)
        {
            int x=sc.nextInt();
            if(x==0) break;
            for(int i=1;i<=x;i++) System.out.print(i+" ");
            System.out.println("");
        }
    }
}

720. 连续整数相加 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int x=sc.nextInt();
        int sum=0;
        while(true)
        {
            int n=sc.nextInt();
            if(n>0) 
            {
                for(int i=x;i<x+n;i++) sum+=i;
                break;
            }
        }
        System.out.print(sum);
    }
}

723. PUM - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int m=sc.nextInt(),n=sc.nextInt();
        int cnt=1;
        for(int i=1;i<=m;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(j==n) System.out.print("PUM");
                else System.out.printf("%d ",cnt);
                cnt++;
            }
            System.out.println("");
        }
    }
}

710. 六个奇数 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int cnt=0;
        for(int i=n;cnt<6;i++)
            if(i%2==1) {System.out.println(i);cnt++;}
    }
}

711. 乘法表 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        for(int i=1;i<=10;i++)
        {
            System.out.println(i+" x "+n+" = "+i*n);
        }
    }
}

718. 实验 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int sum=0,s1=0,s2=0,s3=0;
        while(n-->0)
        {
            int x=sc.nextInt();
            sum+=x;
            String s=sc.next();
            if("C".equals(s)) s1+=x;
            else if("R".equals(s)) s2+=x;
            else s3+=x;
        }
        System.out.println("Total: "+sum+" animals");
        System.out.println("Total coneys: "+s1);
        System.out.println("Total rats: "+s2);
        System.out.println("Total frogs: "+s3);
        System.out.printf("Percentage of coneys: %.2f %%\n",s1*100.0/sum);
        System.out.printf("Percentage of rats: %.2f %%\n",s2*100.0/sum);
        System.out.printf("Percentage of frogs: %.2f %%\n",s3*100.0/sum);
    }
}

713. 区间 2 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int in=0,out=0;
        while(n-->0)
        {
            int x=sc.nextInt();
            if(x>=10&&x<=20) in++;
            else out++;
        }
        System.out.println(in+" in");
        System.out.println(out+" out");
    }
}

719. 连续奇数的和 2 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        while(n-->0)
        {
            int a=sc.nextInt(),b=sc.nextInt();
            int sum=0;
            if(a>b) 
            {
                int t=a;
                a=b;
                b=t;
            }
            for(int i=a+1;i<b;i++) if(i%2!=0) sum+=i;
            System.out.println(sum);
        }
    }
}

!简单斐波那契

717. 简单斐波那契 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int a=0,b=1;
        while(n-->0)
        {
            System.out.print(a+" ");
            int c=a+b;
            a=b;b=c;
        }
    }
}

 722. 数字序列和它的和 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        while(true)
        {
            int a=sc.nextInt(),b=sc.nextInt();
            if(a<=0||b<=0) break;
            int sum=0;
            for(int i=(a>b?b:a);i<=(a>b?a:b);i++)
            {
                sum+=i;
                System.out.print(i+" ");
            }
            System.out.println("Sum="+sum);
        }
    }
}

!试除法求约数

725. 完全数 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        while(n-->0)
        {
            int x=sc.nextInt();
            int sum=-x; //题目要求不包括自己本身
            for(int i=1;i<=x/i;i++)//经典试除法求约数
                if(x%i==0)
                {
                    sum+=i; 
                    if(i!=x/i) sum+=x/i; //如果不是平方数 就把它配对的数也放进去
                }  
            
            if(sum==x) System.out.println(x+" is perfect");
            else System.out.println(x+" is not perfect");
        }
    }
}

!试除法判断约数
726. 质数 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        while(n-->0)
        {
            boolean f=true;
            int x=sc.nextInt();
            if(x<2)
            {
                System.out.println(x+" is not prime");
                continue;
            }
            for(int i=2;i<=x/i;i++)//经典试除法判断质数
                if(x%i==0)
                {
                    f=false;
                    break;
                }
            
            if(f) System.out.println(x+" is prime");
            else System.out.println(x+" is not prime");
        }
    }
}

 !菱形 - 困难

727. 菱形 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int x=n/2;
        for(int i=-x;i<=x;i++)
        {
            for(int j=0;j<Math.abs(i);j++) System.out.print(" ");
            for(int j=0;j<n-Math.abs(i)*2;j++) System.out.print("*");
            System.out.println("");
        }
    }
}

曼哈顿距离

d = | xi-xj | + | yi-yj | 

打印星号的坐标(i,j)满足到矩阵中心点的d <=n/2

即 abs(i-n/2)+abs(j-n/2)<=n/2

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
                if(Math.abs(i-n/2)+Math.abs(j-n/2)<=n/2) 
                    System.out.print("*");
                else System.out.print(" ");
            System.out.println("");
        }
    }
}

三、数组

一维数组 

int[] a=new int[10];
double[] b=new double[];

二维数组

737. 数组替换 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int[]a=new int[10];
        for(int i=0;i<10;i++)
        {
            a[i]=sc.nextInt();
            if(a[i]<=0) a[i]=1;
            System.out.printf("X[%d] = %d\n",i,a[i]);
        }
    }
}

738. 数组填充 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int[]a=new int[10];
        a[0]=sc.nextInt();
        for(int i=1;i<10;i++) 
            a[i]=a[i-1]*2;
        for(int i=0;i<10;i++) System.out.printf("N[%d] = %d\n",i,a[i]);
    }
}

 739. 数组选择 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        double[]a=new double[100];
        for(int i=0;i<100;i++)
        {
            a[i]=sc.nextDouble();
            if(a[i]<=10) System.out.printf("A[%d] = %.1f\n",i,a[i]);
        }
    }
}

743. 数组中的行 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        String c=sc.next();
        double sum=0;
        double[][] a=new double[12][12];
        for(int i=0;i<12;i++)
            for(int j=0;j<12;j++)
            {
                a[i][j]=sc.nextDouble();
                if(i==n) sum+=a[n][j];    
            }
        if("S".equals(c)) System.out.printf("%.1f",sum);
        else System.out.printf("%.1f",sum/12);
    }
}

 745. 数组的右上半部分 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        String c=sc.next();
        double sum=0;
        double[][] a=new double[12][12];
        for(int i=0;i<12;i++)
            for(int j=0;j<12;j++)
            {
                a[i][j]=sc.nextDouble();
                if(j>i) sum+=a[i][j];    
            }
        if("S".equals(c)) System.out.printf("%.1f",sum);
        else System.out.printf("%.1f",sum/66);
    }
}

 747. 数组的左上半部分 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        String c=sc.next();
        double sum=0;
        double[][] a=new double[12][12];
        for(int i=0;i<12;i++)
            for(int j=0;j<12;j++)
            {
                a[i][j]=sc.nextDouble();
                if(i+j<11) sum+=a[i][j];    
            }
        if("S".equals(c)) System.out.printf("%.1f",sum);
        else System.out.printf("%.1f",sum/66);
    }
}

 749. 数组的上方区域 - AcWing题库

左上半部分和右上半部分取交集

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        String c=sc.next();
        double sum=0;
        double[][] a=new double[12][12];
        for(int i=0;i<12;i++)
            for(int j=0;j<12;j++)
            {
                a[i][j]=sc.nextDouble();
                if(j>i&&i+j<11) sum+=a[i][j];    
            }
        if("S".equals(c)) System.out.printf("%.1f",sum);
        else System.out.printf("%.1f",sum/30);
    }
}

751. 数组的左方区域 - AcWing题库

左上半部分和左下半部分取交集

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        String c=sc.next();
        double sum=0;
        double[][] a=new double[12][12];
        for(int i=0;i<12;i++)
            for(int j=0;j<12;j++)
            {
                a[i][j]=sc.nextDouble();
                if(i>j&&i+j<11) sum+=a[i][j];    
            }
        if("S".equals(c)) System.out.printf("%.1f",sum);
        else System.out.printf("%.1f",sum/30);
    }
}

!回字形方阵

753. 平方矩阵 I - AcWing题库

某个位置是几取决于该位置距离上下左右四个方向的最短距离

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        while(true)
        {
            int n=sc.nextInt();
            if(n==0) break;
            
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<n;j++)
                {
                    int x=Math.min(i+1,n-i);//该位置距离上下边界的最小值
                    int y=Math.min(j+1,n-j);//该位置距离左右边界的最小值
                    int z=Math.min(x,y);
                    System.out.print(z+" ");
                }
                System.out.println("");
            }
            System.out.println("");
        }
    }
}

 740. 数组变换 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int[] a=new int[20];
        for(int i=0;i<20;i++) a[i]=sc.nextInt();
        for(int i=0;i<20;i++) System.out.printf("N[%d] = %d\n",i,a[19-i]);
    }
}

741. 斐波那契数列 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        long[] a=new long[61];
        int n=sc.nextInt();
        long x=0,y=1;
        a[1]=1;
        for(int i=2;i<=60;i++) 
        {
            long c=x+y;
            x=y;
            y=c;
            a[i]=c;
        }
        while(n-->0)
        {
            int t=sc.nextInt();
            System.out.printf("Fib(%d) = %d\n",t,a[t]);
        }
    }
}

742. 最小数和它的位置 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int[] a=new int[n];
        int minx=1002;
        for(int i=0;i<n;i++)
        {
            a[i]=sc.nextInt();
            minx=Math.min(minx,a[i]);
        }
        for(int i=0;i<n;i++)
            if(a[i]==minx) 
            {
                System.out.printf("Minimum value: %d\nPosition: %d",minx,i);
                break;
            }
    }
}

744. 数组中的列 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        double[][] a=new double[12][12];
        int x=sc.nextInt();
        String c=sc.next();
        double sum=0;
        for(int i=0;i<12;i++)
            for(int j=0;j<12;j++) a[i][j]=sc.nextDouble();
        
        for(int i=0;i<12;i++) sum+=a[i][x];
        if("S".equals(c)) System.out.printf("%.1f",sum);
        else System.out.printf("%.1f",sum/12);
    }
}

748. 数组的右下半部分 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        String c=sc.next();
        double sum=0;
        double[][] a=new double[12][12];
        for(int i=0;i<12;i++)
            for(int j=0;j<12;j++)
            {
                a[i][j]=sc.nextDouble();
                if(i+j>11) sum+=a[i][j];    
            }
        if("S".equals(c)) System.out.printf("%.1f",sum);
        else System.out.printf("%.1f",sum/66);
    }
}

746. 数组的左下半部分 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        String c=sc.next();
        double sum=0;
        double[][] a=new double[12][12];
        for(int i=0;i<12;i++)
            for(int j=0;j<12;j++)
            {
                a[i][j]=sc.nextDouble();
                if(i>j) sum+=a[i][j];    
            }
        if("S".equals(c)) System.out.printf("%.1f",sum);
        else System.out.printf("%.1f",sum/66);
    }
}

750. 数组的下方区域 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        String c=sc.next();
        double sum=0;
        double[][] a=new double[12][12];
        for(int i=0;i<12;i++)
            for(int j=0;j<12;j++)
            {
                a[i][j]=sc.nextDouble();
                if(i>j&&i+j>11) sum+=a[i][j];    
            }
        if("S".equals(c)) System.out.printf("%.1f",sum);
        else System.out.printf("%.1f",sum/30);
    }
}

752. 数组的右方区域 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        String c=sc.next();
        double sum=0;
        double[][] a=new double[12][12];
        for(int i=0;i<12;i++)
            for(int j=0;j<12;j++)
            {
                a[i][j]=sc.nextDouble();
                if(j>i&&i+j>11) sum+=a[i][j];    
            }
        if("S".equals(c)) System.out.printf("%.1f",sum);
        else System.out.printf("%.1f",sum/30);
    }
}

!斜对角线矩阵

754. 平方矩阵 II - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        while(true)
        {
            int n=sc.nextInt();
            if(n==0) break;
            
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<n;j++)
                {
                    System.out.print(Math.abs(i-j)+1+" ");
                }
                System.out.println("");
            }
            System.out.println("");
        }
    }
}

755. 平方矩阵 III - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        while(true)
        {
            int n=sc.nextInt();
            if(n==0) break;
            int[][] a=new int[n][n];
            for(int i=0;i<n;i++)
                for(int j=0;j<n;j++)
                    a[i][j]=1<<i+j; //意思是将1向左移i+j位 等价于2^(i+j)
            
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<n;j++)
                {
                    System.out.print(a[i][j]+" ");
                }
                System.out.println("");
            }
                
            System.out.println("");
        }
    }
}

!蛇形矩阵

756. 蛇形矩阵 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt(),m=sc.nextInt();
        int[][] g=new int[n][m];
        int[] dx={-1,0,1,0},dy={0,1,0,-1};//1号位置对应右方向 顺序应为:右-下-左-上
        int x=0,y=0,d=1;
        for(int i=1;i<=n*m;i++)
        {
            g[x][y]=i;
            int a=dx[d]+x,b=dy[d]+y;
            if(a<0||a>=n||b<0||b>=m||g[a][b]>0)//如果越界或重复 则换个方向走
            {
                d=(d+1)%4;
                a=x+dx[d];
                b=y+dy[d];
            }
            x=a;
            y=b;
        }
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
                System.out.print(g[i][j]+" ");
            System.out.println();
        }
    }
}

四、字符串

  • s.length() 字符串的长度
  • sc.next() 遇到空格或回车会终止读入
  • sc.nextLine() 遇到空格回车不会终止读入
  • s.indexOf(char c)、s.indexOf(String str)、s.lastIndexOf(char c)、s.lastIndexOf(String str):查找,找不到返回-1
  • s.toCharArray()  方法将字符串转换为字符数组
  • s.replace(char oldChar, char newChar):替换字符
    s.replace(String oldRegex, String newRegex):替换字符串
  • s.substring(begin,end)  返回截取段字符串 不包括end 所以一般要+1
  • sc.hasNext()  表示你是否有输入数据 有返回true 没有返回false
  • s.charAt( i )相当于c++里的s[ i ]
  • a.compareTo(b) 比较两字符串字典序大小  相等返回0  a>b返回1  a<b返回-1
  • toLowerCase():全部用小写字符
    toUpperCase():全部用大写字符
  • s.split(String regex) —— 分割字符串
  • 注意:“.”和“|”都是转义字符,必须得加"\\"。例如: 

    使用“.”作为分隔符。正确写法String.split("\\.");不能使用String.split("."); 
    使用“|”作为分隔符。正确写法String.split("\\|");不能使用String.split("|"); 

  • s.endsWith(String regex):判断是否以某个后缀结尾

  • s.equals(b) 判断两字符串==

760. 字符串长度 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        String s=sc.nextLine();
        System.out.print(s.length());
    }
}

 761. 字符串中的数字个数 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        String s=sc.nextLine();
        int cnt=0;
        for(char c:s.toCharArray())
            if(c>='0'&&c<='9') cnt++;
        System.out.print(cnt);
    }
}

763. 循环相克令 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        while(n-->0)
        {
            String s1=sc.next(),s2=sc.next();
            int x=0,y=0;
            
            if("Hunter".equals(s1)) x=1;
            else if("Bear".equals(s1)) x=2;
            else x=3;
            
            if("Hunter".equals(s2)) y=1;
            else if("Bear".equals(s2)) y=2;
            else y=3;
            
            if(x==y) System.out.println("Tie");
            else if((x==1&&y==3)||(x==2&&y==1)||(x==3&&y==2)) System.out.println("Player1");
            else System.out.println("Player2");
        }
    }
}

 765. 字符串加空格 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        String s=sc.nextLine();
        for(char c:s.toCharArray())
            System.out.print(c+" ");
    }
}

769. 替换字符 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        String s=sc.next();
        String c=sc.next();
        System.out.print(s.replace(c,"#"));
    }
}

773. 字符串插入 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext())
        {
            String a=sc.next(),b=sc.next();
            int k=0;
            for(int i=0;i<a.length();i++)
                if(a.charAt(i)>a.charAt(k)) k=i;
            System.out.println(a.substring(0,k+1)+b+a.substring(k+1));
        }
    }
}

 772. 只出现一次的字符 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        String s=sc.next();
        int[] cnt=new int[26];
        for(char c:s.toCharArray())
            cnt[c-'a']++;
        for(char c:s.toCharArray())
            if(cnt[c-'a']==1) {System.out.print(c);return;}
        System.out.print("no");
    }
}

 字符串匹配 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        Double n=sc.nextDouble();
        String a=sc.next(),b=sc.next();
        int cnt=0;
        for(int i=0;i<a.length();i++)
            if(a.charAt(i)==b.charAt(i)) cnt++;
        if((double)cnt/a.length()>=n) System.out.print("yes");
        else System.out.print("no");
    }
}

768. 忽略大小写比较字符串大小 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        String a=sc.nextLine().toLowerCase();
        String b=sc.nextLine().toLowerCase();
        
        int res=a.compareTo(b);
        if(res==0) System.out.print("=");
        else if(res<0) System.out.print("<");
        else System.out.print(">");
    }
}

 766. 去掉多余的空格 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext())
        {
            System.out.printf("%s ",sc.next()); //输出输入的字符串+空格 sc.next()遇到空格自动跳过
        }
    }
}

767. 信息加密 - AcWing题库

c-'a' → 把a~z对应0~25

c-'a'+1 → 把a~z对应1~26

(c-'a'+1)%26 → z对应0也就是对应a

(c-'a'+1)%26+'a' → 把整体转化为字符

(char)((c-'a'+1)%26+'a') → 因为系统问题 所以还需要显式转换一下

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        String s=sc.nextLine();
        for(char c:s.toCharArray())
        {
            if(c>='a'&&c<='z') c=(char)((c-'a'+1)%26+'a');
            else if(c>='A'&&c<='Z') c=(char)((c-'A'+1)%26+'A');
            System.out.print(c);
        }
    }
}

764. 输出字符串 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        String s=sc.nextLine();
        String res="";
        for(int i=0;i<s.length();i++)
            res+=(char)(s.charAt(i)+s.charAt((i+1)%s.length()));
        System.out.print(res);
    }
}

770. 单词替换 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        String[] s=sc.nextLine().split(" ");//用一个string数组 用空格隔断s
        String a=sc.next(),b=sc.next();
        for(String str:s)
        {
            if(a.equals(str)) System.out.print(b);//遍历s 如果出现和a相同的串 输出b
            else System.out.print(str);//否则输出原串
            System.out.print(" ");
        }
    }
}

!双指针算法 - 字符串中最长的连续出现的字符
771. 字符串中最长的连续出现的字符 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        while(n-->0)
        {
            String s=sc.next();
            char res=' ';
            int cnt=0;
            for(int i=0;i<s.length();)//枚举一下每个起点
            {
                int j=i+1;
                while(j<s.length()&&s.charAt(i)==s.charAt(j)) j++;//把j指针移到末尾
            
                if(j-i>cnt)//更新最大长度
                {
                    cnt=j-i;
                    res=s.charAt(i);
                }
                i=j;//起始指针移至下一段
            }
            System.out.println(res+" "+cnt);
        }
    }
}

774. 最长单词 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        String[] s=sc.nextLine().split(" ");
        int k=0;
        String res=" ";
        for(String str:s)
        {
            if(str.endsWith(".")) str=str.substring(0,str.length()-1); 
            if(k<str.length())
            {
                k=str.length();
                res=str;
            }
        }
        System.out.print(res);
    }
}

775. 倒排单词 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        String[] s=sc.nextLine().split(" ");
        for(int i=s.length-1;i>=0;i--)
            System.out.print(s[i]+" ");
    }
}

!字符串循环移位包含问题

 776. 字符串移位包含问题 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        String a=sc.next(),b=sc.next();
        if(b.length()>a.length())
        {
            String t=a;
            a=b;
            b=t;
        }
        boolean f=false;
        for(int i=0;i<a.length();i++)
        {
            if(a.indexOf(b)!=-1) {f=true;break;}
            a=a.substring(1)+a.charAt(0);//循环移位
        }
        System.out.print(f);
    }
}

!字符串的乘方 

777. 字符串乘方 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        while(true)
        {
            String s=sc.next();
            if(s.equals(".")) break;
            
            int n=s.length();
            for(int d=1;d<=n;d++)//隔断d从小到大枚举 题目要求最大周期数 隔断越小 周期数越大
                if(n%d==0)
                {
                    String str=s.substring(0,d);
                    String res="";
                    for(int j=0;j<n/d;j++) res+=str;
                    if(res.equals(s))
                    {
                        System.out.println(n/d);
                        break;
                    }
                }
        }
    }
}

!indexOf和lastIndexOf的应用

778. 字符串最大跨距 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        String[] str=sc.next().split(",");
        String s=str[0],a=str[1],b=str[2];
        int x=s.indexOf(a),y=s.lastIndexOf(b);//x是a串在s串里最左边出现的第一个字母的下标 y是b串在s串里最右边出现的……
        if(x!=-1&&y!=-1&&y>x+a.length()-1)
        {
            System.out.print(y-(x+a.length()-1)-1);//最外面-1是因为如果a b串挨着 答案应该是0
        }
        else System.out.print(-1);
    }
}

779. 最长公共字符串后缀 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        while(true)
        {
            int n=sc.nextInt();
            if(n==0) break;
            String[] str=new String[n];
            for(int i=0;i<n;i++) str[i]=sc.next();
            
            StringBuilder res=new StringBuilder("");
            for(int i=0;i<str[0].length();i++)
            {
                char c=str[0].charAt(str[0].length()-1-i);
                boolean f=true;
                for(int j=1;j<n;j++)
                    if(i>=str[j].length()||c!=str[j].charAt(str[j].length()-1-i))
                    //当其余串比公共后缀小,或后缀有字符不同时退出
                    {
                       f=false; 
                       break;
                    }
                if(f) res.append(c);
                else break;
            }
            res.reverse();
            System.out.println(res);
        }
    }
}

五、函数

804. n的阶乘 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int x=sc.nextInt();
        int res=fact(x);
        System.out.print(res);
    }
    
    public static int fact(int x) //静态函数中只能调用静态函数/变量
    {
        int sum=1;
        for(int i=x;i>0;i--) sum*=i;
        return sum;
    }
}

805. x和y的最大值 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int x=sc.nextInt(),y=sc.nextInt();
        int res=max(x,y);
        System.out.print(res);
    }
    
    public static int max(int x,int y) //静态函数中只能调用静态函数/变量
    {
        return x>y? x:y;
    }
}

欧几里得求最大公约数gcd

808. 最大公约数 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int x=sc.nextInt(),y=sc.nextInt();
        int res=gcd(x,y);
        System.out.print(res);
    }
    
    public static int gcd(int x,int y) //静态函数中只能调用静态函数/变量
    {
        return y>0? gcd(y,x%y):x;
    }
}

811. 交换数值 - AcWing题库

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int[] a={sc.nextInt(),sc.nextInt()};
        swap(a);
        System.out.print(a[0]+" "+a[1]);
    }
    
    public static void swap(int[] a) //静态函数中只能调用静态函数/变量
    {
        int t=a[0];
        a[0]=a[1];
        a[1]=t;
    }
}

812. 打印数字 - AcWing题库

import java.util.*;

public class Main
{
    static void print(int[] a,int size)
    {
        for(int i=0;i<size;i++) System.out.print(a[i]+" ");
    }
    
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt(),len=sc.nextInt();
        int[] a=new int[n];
        for(int i=0;i<n;i++) a[i]=sc.nextInt();
        print(a,len);
    }
}

813. 打印矩阵 - AcWing题库

import java.util.*;

public class Main
{
    static void print2D(int[][]a,int n,int m)
    {
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++) System.out.print(a[i][j]+" ");
            System.out.println();
        }
    }
    
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt(),m=sc.nextInt();
        int[][] a=new int[n][m];
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++) a[i][j]=sc.nextInt();
        print2D(a,n,m);
    }
}

819. 递归求阶乘 - AcWing题库

import java.util.*;

public class Main
{
    static int f(int n)
    {
        if(n==1) return 1;
        return n*f(n-1);
    }
    
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        System.out.print(f(n));
    }
}

820. 递归求斐波那契数列 - AcWing题库

import java.util.*;

public class Main
{
    static int f(int n)
    {
        if(n==1||n==2) return 1;
        return f(n-1)+f(n-2);
    }
    
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        System.out.print(f(n));
    }
}

810. 绝对值 - AcWing题库

import java.util.*;

public class Main
{
    static int abs(int n)
    {
        if(n<0) return -1*n;
        return n;
    }
    
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        System.out.print(abs(n));
    }
}

806. 两个数的和 - AcWing题库

import java.util.*;

public class Main
{
    static double add(double a,double b)
    {
        return a+b;
    }
    
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        double a=sc.nextDouble(),b=sc.nextDouble();
        System.out.printf("%.2f",add(a,b));
    }
}

807. 区间求和 - AcWing题库

import java.util.*;

public class Main
{
    static int sum(int l,int r)
    {
        int num=0;
        for(int i=l;i<=r;i++) num+=i;
        return num;
    }
    
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int l=sc.nextInt(),r=sc.nextInt();
        System.out.print(sum(l,r));
    }
}

809. 最小公倍数 - AcWing题库

import java.util.*;

public class Main
{
    static int gcd(int a,int b)
    {
        return b==0? a:gcd(b,a%b);
    }
    
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int l=sc.nextInt(),r=sc.nextInt();
        System.out.print(l*r/gcd(l,r));
    }
}

814. 复制数组 - AcWing题库

import java.util.*;

public class Main
{
    
    static void copy(int[] a,int[] b,int k)
    {
        for(int i=0;i<k;i++) b[i]=a[i];
    }
    
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt(),m=sc.nextInt(),k=sc.nextInt();
        int[] a=new int[n],b=new int[m];
        for(int i=0;i<n;i++) a[i]=sc.nextInt();
        for(int j=0;j<m;j++) b[j]=sc.nextInt();
        copy(a,b,k);
        for(int i=0;i<m;i++) System.out.print(b[i]+" ");
    }
}

815. 打印字符串 - AcWing题库

import java.util.*;

public class Main
{
    
    static void print(String s)
    {
        System.out.print(s);
    }
    
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        String s=sc.nextLine();
        print(s);
    }
} 

816. 数组翻转 - AcWing题库 - 双指针

import java.util.*;

public class Main
{
    static void reverse(int[] a,int k)
    {
        for(int i=0,j=k-1;i<j;i++,j--)
        {
            int t=a[i];
            a[i]=a[j];
            a[j]=t;
        }
    }
    
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt(),k=sc.nextInt();
        int[] a=new int[n];
        for(int i=0;i<n;i++) a[i]=sc.nextInt();
        reverse(a,k);
        for(int i=0;i<n;i++) System.out.print(a[i]+" ");
    }
} 

817. 数组去重 - AcWing题库

1、常规哈希表法

import java.util.*;

public class Main
{
    static int guc(int[] a,int n)
    {
        int res=0;
        int[] b=new int[1010];
        for(int i=0;i<n;i++)
            if(b[a[i]]==0)
            {
                b[a[i]]++;
                res++;
            }
        return res;
        
    }
    
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int[] a=new int[n];
        for(int i=0;i<n;i++) a[i]=sc.nextInt();
        System.out.print(guc(a,n));
    }
} 

2、STL大法

import java.util.*;

public class Main
{
    static int guc(int[] a,int n)
    {
        Set<Integer> st=new HashSet<>();
        for(int i=0;i<n;i++)
            st.add(a[i]);
        return st.size();
    }
    
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int[] a=new int[n];
        for(int i=0;i<n;i++) a[i]=sc.nextInt();
        System.out.print(guc(a,n));
    }
} 

818. 数组排序 - AcWing题库

import java.util.*;

public class Main
{
    static void sort(int[] a,int l,int r)
    {
        for(int i=l;i<=r;i++)
            for(int j=i+1;j<=r;j++)
                if(a[i]>a[j])
                {
                    int t=a[i];
                    a[i]=a[j];
                    a[j]=t;
                }
    }
    
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt(),l=sc.nextInt(),r=sc.nextInt();
        int[] a=new int[n];
        for(int i=0;i<n;i++) a[i]=sc.nextInt();
        sort(a,l,r);
        for(int i=0;i<n;i++)
        System.out.print(a[i]+" ");
    }
} 

!821.跳台阶 - dp / 斐波那契数列

821. 跳台阶 - AcWing题库

本问题其实常规解法可以分成多个子问题,爬第n阶楼梯的方法数量,等于 2 部分之和

  • 爬上 n-1 阶楼梯的方法数量。因为再爬1阶就能到第n阶
  • 爬上 n-2 阶楼梯的方法数量。因为再爬2阶就能到第n阶

所以我们得到公式 dp[n] = dp[n-1] + dp[n-2]
同时需要初始化 dp[0]=1和dp[1]=1

第0阶台阶1种方法 第1阶台阶1种方法 

import java.util.*;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int[] dp=new int[n+1];
        dp[0]=1;
        dp[1]=1;
        for(int i=2;i<=n;i++) dp[i]=dp[i-1]+dp[i-2];
        System.out.print(dp[n]);
    }
} 

其实就是斐波那契数列

1 2 3 5 8 13 

import java.util.*;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        if(n<=2)
        {
            System.out.print(n);
            return;
        }
        int a=1,b=2;
        for(int i=3;i<=n;i++)
        {
            int t=a+b;
            a=b;
            b=t;
        }
        System.out.print(b);
    }
} 

!822.走方格 - dp 

822. 走方格 - AcWing题库

状态表示dp[i][j] 表示从(0,0)开始走到(i,j)有多少种方案

因为每次只能往右和往下走 所以 dp[i][0]=dp[0][j]=1

状态转移方程dp[i][j]=dp[i-1][j]+dp[i][j-1]

import java.util.*;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt(),m=sc.nextInt();
        int[][] dp=new int[11][11];
        for(int i=0;i<=n;i++)
            for(int j=0;j<=m;j++)
                if(i==0||j==0) dp[i][j]=1;
                else dp[i][j]=dp[i-1][j]+dp[i][j-1];
        System.out.print(dp[n][m]);
    }
} 

!823.排列 - dfs

823. 排列 - AcWing题库

import java.util.*;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;


public class Main
{
    private static int n;
    private static boolean[] st;
    private static int[] path;
    private static BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
    
    private static void dfs(int u) throws Exception
    {
        if(u==n)
        {
            for(int i=0;i<n;i++) bw.write(path[i]+" ");
            bw.write("\n");
            return;
        }
        for(int i=1;i<=n;i++)
            if(!st[i])
            {
                path[u]=i;
                st[i]=true;
                dfs(u+1);
                st[i]=false;
            }
    }
    
    public static void main(String[] args) throws Exception
    {
        Scanner sc=new Scanner(System.in);
        n=sc.nextInt();
        path=new int[n];
        st=new boolean[n+1];
        dfs(0);
        bw.flush();
    }
} 

六、类与接口

! 斐波那契三个版本写法

21. 斐波那契数列 - AcWing题库

1、dp版

class Solution {
    public int Fibonacci(int n) {
         int[] f=new int[40];
         f[0]=0;
         f[1]=1;
         for(int i=2;i<=n;i++) f[i]=f[i-1]+f[i-2];
         return f[n];
    }
}

2、轮转数组

class Solution {
    public int Fibonacci(int n) {
         if(n<=1) return n;
         int a=0,b=1;
         for(int i=2;i<=n;i++)
         {
             int t=a+b;
             a=b;
             b=t;
         }
         return b;
    }
}

3、递归版

class Solution {
    public int Fibonacci(int n) {
         if(n<=1) return n;
         return Fibonacci(n-1)+Fibonacci(n-2);
    }
}

16. 替换空格 - AcWing题库

class Solution {
    public String replaceSpaces(StringBuffer str) {
        return str.toString().replace(" ","%20");
    }
}

84. 求1+2+…+n - AcWing题库

逻辑表达式短路原则

_____&&______ 如果前面为false 就不会执行后面

class Solution {
    public int getSum(int n) {
        int res=n;
        boolean f= n>0 && (res+=getSum(n-1))>0;
        return res;
    }
}

!在O(1)时间删除链表节点

28. 在O(1)时间删除链表结点 - AcWing题库

由于是单链表,我们不能找到前驱节点,所以我们不能按常规方法将该节点删除。
我们可以换一种思路,将下一个节点的值复制到当前节点,然后将下一个节点删除即可。

class Solution {
    public void deleteNode(ListNode node) {
        node.val=node.next.val;
        node.next=node.next.next;
    }
}

17. 从尾到头打印链表 - AcWing题库

class Solution {
    public int[] printListReversingly(ListNode head) {
        int n=0;
        ListNode p=head;
        while(p!=null)
        {
            n++;
            p=p.next;
        }
        int[] res=new int[n];
        for(ListNode t=head;t!=null;t=t.next)
            res[--n]=t.val;
        return res;
    }
}

36. 合并两个排序的链表 - AcWing题库

class Solution {
    public ListNode merge(ListNode l1, ListNode l2) {
        ListNode dummy=new ListNode(-1); //虚拟头结点
        ListNode res=dummy;
        while(l1!=null&&l2!=null)
        {
            if(l1.val<=l2.val)
            {
                 res.next=l1;
                 res=res.next;
                 l1=l1.next;
            }
            else
            {
                res.next=l2;
                res=res.next;
                l2=l2.next;
            }
        }
        if(l1!=null) res.next=l1;
        else res.next=l2;
        
        return dummy.next;
    }
}

78. 左旋转字符串 - AcWing题库

class Solution {
    public String leftRotateString(String str,int n) {
        String res="";
        res+=str.substring(n);
        res+=str.substring(0,n);
        return res;
    }
}
class Solution {
    public String leftRotateString(String str,int n) {
        StringBuffer res=new StringBuffer();
        res.append(str.substring(n));
        res.append(str.substring(0,n));
        return res.toString();
    }
}

 87. 把字符串转换成整数 - AcWing题库

class Solution {
    public int strToInt(String str) {
        str=str.trim();
        int n=str.length();
        if(n==0) return 0;
        long sum=0;
        int cnt=0,x=1;
        if(str.charAt(0)=='+'||str.charAt(0)=='-')
        {
            if(str.charAt(0)=='-') x*=-1;
            str=str.substring(1);
        }
        for(char c:str.toCharArray())
        {
            if(c<'0'||c>'9') break;
            if(c>='0'&&c<='9') 
            {
                sum=sum*10+(c-'0');
                cnt++;
                if(sum>Integer.MAX_VALUE) break; //不break 可能爆llong
            }
        }
        sum*=x;
            
        if(cnt==0) return 0;
        if(sum>Integer.MAX_VALUE) return Integer.MAX_VALUE;
        if(sum<Integer.MIN_VALUE) return Integer.MIN_VALUE;
        return (int)sum;
    }
}

!35.反转链表

35. 反转链表 - AcWing题库

class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode pre=null;
        ListNode cur=head;
        
        while(cur!=null)
        {
            ListNode t=cur.next;
            cur.next=pre;
            pre=cur;
            cur=t;
        }
        return pre;
    }
}

!66.两个链表的第一个公共节点

66. 两个链表的第一个公共结点 - AcWing题库

class Solution {
    public ListNode findFirstCommonNode(ListNode headA, ListNode headB) {
        ListNode p=headA,q=headB;
        while(p!=q)  //a+c+b=b+c+a
        {
            p= p==null? headB:p.next;
            q= q==null? headA:q.next;
        }
        return p;
    }
}

!29.删除链表重复节点

29. 删除链表中重复的节点 - AcWing题库

  • 因为头结点也可能被删去 所以添加虚拟头节点
  • 如果q指针指向的节点和p.next所指的值一样 则向后移动q指针
  • 否则p所指节点指向q 实现删去重复元素节点
  • 如果p.next.next=q 说明p和q间只隔了1个节点(该节点不用删去) 则移动p指针
class Solution {
    public ListNode deleteDuplication(ListNode head) {
        ListNode dummy=new ListNode(-1);
        dummy.next=head;
        ListNode p=dummy;
        while(p.next!=null)
        {
            ListNode q=p.next;
            while(q!=null&&q.val==p.next.val) q=q.next;
            
            if(p.next.next==q) p=p.next; //如果p q间只隔一个点 则说明中间这个点 不是重复 则移动p指针
            else p.next=q; //否则删去中间重复节点
        }
        return dummy.next;
    }
}

 !类似于c++结构体排序 862.三元组排序

862. 三元组排序 - AcWing题库

import java.util.*;

class data implements Comparable<data>
{
    int x;
    double y;
    String s;
    
    data(int x,double y,String s)
    {
        this.x=x;
        this.y=y;
        this.s=s;
    }
    
    public int compareTo(data t)
    {
        return x-t.x; //升序
    }
}

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        
        data[] d=new data[n];
        
        for(int i=0;i<n;i++)
            d[i]=new data(sc.nextInt(),sc.nextDouble(),sc.next());
            
        Arrays.sort(d);
        
        for(data t:d) System.out.printf("%d %.2f %s\n",t.x,t.y,t.s);
    }
}

七、常用容器

​​​​​​​【Java常用容器】Map+Set+栈+队列+List+StringBuilder+stream流_Roye_ack的博客-CSDN博客_mp.put(ch, (mp.getordefault(ch, 0)) + 1);

1、栈

Deque<Integer> stack=new LinkedList<>();

stack.push(x); //入栈

stack.pop(); //出栈 删除并返回栈顶值
res=stack.pop();

stack.peek(); //返回栈顶值

stack.isEmpty(); //判空

stack.clear(); //清空

活动 - AcWing

import java.util.*;

class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        Deque<Integer> stk=new LinkedList<>();
        while(n-->0)
        {
            String s=sc.next();
            if(s.equals("push"))
            {
                int x=sc.nextInt();
                stk.push(x);
            }else if(s.equals("pop"))
            {
                stk.pop();
            }
            else if(s.equals("empty"))
            {
                if(stk.isEmpty()) System.out.println("YES");
                else System.out.println("NO");
            }
            else
            {
                System.out.println(stk.peek());
            }
        }
    }
}

2、Map

(1)HashMap

Map<Integer,Integer> mp=new HashMap<>();
Map<Character,Integer> mp=new HashMap<>();

//将key-value添加到map中
mp.put(key值,val值); 

char ch;
mp.put(ch,mp.getOrDefault(ch,0)+1); //相当于mp[ch]++;
mp.put(ch,mp.getOrDefault(ch,0)-1); //相当于mp[ch]--;

mp.putAll(Map m); //将m中的所有key-value添加到map中

mp.remove(key); //删除key-val对

mp.clear(); //清空

mp.getOrDefault(key,0); //获取key值所对应的value值 如果不存在返回0

//查询是否存在key或value值
mp.containsKey(key);
mp.containsValue(Value);

mp.replace(key,value) //替换指定key对应的value值
mp.replace(key,oldValue,newValue) //当指定key的对应的value为指定值时 替换该值为新值

mp.size();

mp.isEmpty();

(2)TreeMap - 补充

TreeMap中的元素默认按照keys的自然排序排列。

  • 对Integer来说,其自然排序就是数字的升序
  • 对String来说,其自然排序就是按照字母表排序
TreeMap<Integer,String> mp = new TreeMap<>();

//按key值从大到小排列
TreeMap<Integer,String> mp = new TreeMap<>(Comparator.reverseOrder()); 

mp.ceilingEntry(key) //返回大于等于key的最小元素,不存在则返回null
mp.floorEntry(key) //返回小于等于key的最大元素,不存在则返回null

(3)Map的遍历

1、entry法

for(Map.Entry<String,String> x: map.entrySet()) 
{
    String Key = x.getKey();
    String Value = x.getValue();
    System.out.println(Key + ":" + Value);
}

 2、只输出key或value时

// 打印键集合
for(String key : map.keySet()) 
    System.out.println(key);

// 打印值集合
for(String value : map.values()) 
    System.out.println(value);

3、将map转化为set输出 

Set st=mp.keySet();
System.out.print(st);

67. 数字在排序数组中出现的次数 - AcWing题库

class Solution {
    public int getNumberOfK(int[] nums, int k) {
        Map<Integer,Integer> mp=new HashMap<>();
        for(int x:nums) mp.put(x,mp.getOrDefault(x,0)+1);
        int res=mp.getOrDefault(k,0);
        return res;
    }
}

68. 0到n-1中缺失的数字 - AcWing题库

class Solution {
    public int getMissingNumber(int[] nums) {
        int sum=0,n=nums.length;
        for(int x:nums) sum+=x;
        int t=n*(n+1)/2-sum;
        return t;
    }
}

32. 调整数组顺序使奇数位于偶数前面 - AcWing题库 - 双指针

class Solution {
    public void reOrderArray(int[] array) {
        int i=0,j=array.length-1;
        while(i<j)
        {
            while(i<j&&array[i]%2==1) i++;
            while(i<j&&array[j]%2==0) j--;
            if(i<j)
            {
                int t=array[i];
                array[i]=array[j];
                array[j]=t;
            }
        }
    }
}

3、队列 + 优先队列 PriorityQueue

Queue<Integer> q=new LinkedList <>();
Queue<int[]> q=new LinkedList <>(); //{i,j}

//入队 offer
q.offer(x);
q.offer(new int[] {i,j});
 
//出队并返回队头元素 poll 
q.poll();
 
//取队头元素
q.peek();
 
//队伍长度
q.size();
 
//判空
q.isEmpty();
 
//清空
q.clear();
 

(1)优先队列

PriorityQueue<Integer> q=new PriorityQueue<>(); //默认小顶堆

PriorityQueue<Integer> q=new PriorityQueue<>((a,b)->(b-a)); //大顶堆

q.contains(val);

Integer[] t=q.toArray(new Integer[n]); //将队列转化为数组

20. 用两个栈实现队列 - AcWing题库

数据结构 - 用两个栈实现队列 - java版 + c++版_Roye_ack的博客-CSDN博客

活动 - AcWing

import java.util.*;

class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        Queue<Integer> q=new LinkedList<>();
        while(n-->0)
        {
            String s=sc.next();
            if(s.equals("push"))
            {
                int x=sc.nextInt();
                q.offer(x);
            }
            else if(s.equals("pop")) q.poll();
            else if(s.equals("empty"))
            {
                if(q.isEmpty()) System.out.println("YES");
                else System.out.println("NO");
            }
            else System.out.println(q.peek());
        }
    }
}

4、Set

Set<Integer> st=new HashSet<>();

st.add(val); //添加
st.addAll(st1);

st.remove(val); //删除

st.clear(); //清空

st.size(); //大小

st.isEmpty(); //判空

st.contains(val); //查找指定元素是否存在
st.containsAll(st1);

st.retainAll(st2); //取交集

(1)TreeSet 

TreeSet<Integer> st=new TreeSet<>();

TreeSet<Integer> st = new TreeSet<>(Comparator.reverseOrder()); //倒序

st.first(); //返回第一个元素
st.last(); //返回最后一个元素

st.pollFirst(); //返回并删除第一个元素
st.pollLast(); //返回并删除最后一个元素

st.floor(x); //返回最后一个≤x的元素
st.ceiling(x); //返回第一个≥x的元素

st.lower(x); //返回最后一个<x的元素
st.higher(x); //返回第一个>x的元素

​​​​​​​53. 最小的k个数 - AcWing题库

class Solution {
    public List<Integer> getLeastNumbers_Solution(int[] input, int k) {
        TreeSet<Integer> st=new TreeSet<>();
        List<Integer> res=new ArrayList<>();
        for(int x:input) st.add(x);
        while(k-->0) res.add(st.pollFirst());
        return res;
    }
}

 75. 和为S的两个数字 - AcWing题库

class Solution {
    public int[] findNumbersWithSum(int[] nums, int target) {
        Set<Integer> st=new HashSet<>();
        for(int x:nums) st.add(x);
        int[] res=new int[2];
        for(int x:nums)
            if(st.contains(target-x)) 
            {
               res[0]=x; 
               res[1]=target-x;
               break;
            }
        return res;
    }
}

136. 邻值查找 - AcWing题库

import java.util.*;

class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        TreeSet<Integer> st=new TreeSet<>();
        Map<Integer,Integer> mp=new HashMap<>();
        int a=sc.nextInt();
        mp.put(a,1);
        st.add(a);
        
        for(int i=2;i<=n;i++)
        {
            int idx=0,res=0x3f3f3f3f;
            int x=sc.nextInt();
            
            Integer t=st.ceiling(x);
            if(t!=null)
            {
                res=t-x;
                idx=mp.get(t);
            }
            
            t=st.floor(x);
            if(t!=null&&res>=x-t) //选较小的那个 
            {
                res=x-t;
                idx=mp.get(t);
            }
            mp.put(x,i);
            st.add(x);
            System.out.println(res+" "+idx);
        }
    }
}

26. 二进制中1的个数 - AcWing题库

class Solution {
    public int NumberOf1(int n) {
        int res=0;
        for(int i=0;i<32;i++) res+=(n>>i)&1;
        return res;
    }
}

lowbit()

lowbit(x) ——  x的二进制最低位1所表示的数

eg:lowbit(10) —— 1010 —— lowbit(10)=2

lowbit(int x)
{
    return x&(-x);
}
class Solution {
    public int NumberOf1(int n) {
        int res=0;
        while(n!=0)
        {
            int x=n&(-n);
            n-=x;
            res++;
        }
        return res;
    }
}

  • 8
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值