LeetCode刷题 - Java常用输入输出

LeetCode刷题 - Java常用输入输出

基本语法

导包

import java.util.Scanner;
//或者直接导入下面两个包
import java.util.*;
import java.io.*;

常用输入

Scanner sc = new Scanner(System.in);
//读一个整数
int n = sc.nextInt();
//读一个字符串,遇到分号则输入终止
String s = sc.next();
//读一个浮点数
double t = sc.nextDouble();
//读一整行,中间可以有多个空格
String s = sc.nextLine(); 

System.out.print(n);//不换行输出
System.out.println(n);

还可以用BufferedReader类输入

import java.io.*;
import java.util.*;
public class Main{
    public static void main(String[] args)throws IOException{
        BufferedReader cin=new BufferedReader(new InputStreamReader(System.in));
        String str1=cin.readLine();//输入一行
        System.out.println(str1);
         
        String str2=cin.readLine();
        int a=Integer.parseInt(str2);//将str2转换为int,并复制给a
        System.out.println(a);
         
        String str3=cin.readLine();
        double b=Double.parseDouble(str3);//将str3转换为double,并复制给b
        System.out.println(b);
    }
}

判断是否还有下一个输入

sc.hasNext()
sc.hasNextInt()
sc.hasNextDouble()
sc.hasNextLine() 

输出

//不换行输出
System.out.print(); 
//在结束的地方会加一个换行
System.out.println(); 
System.out.format();
System.out.printf();

基本案例

1. 输入一个整数,浮点数,输入一个数组

import java.util.Arrays;
import java.util.Scanner;

public class Class_1 {
    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in) ;
        while(reader.hasNext())
        {
            int m = reader.nextInt() ;
            int [] numbers = new int[m] ;
            for(int index=0;index<m;index++)
            {
                numbers[index] = reader.nextInt();
            }
            System.out.println(Arrays.toString(numbers));
        }
        reader.close() ;
    }
}

2. 输入一个矩阵

import java.util.Scanner;
public class Class_2 {
    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        int m = reader.nextInt();
        int n = reader.nextInt() ;
        int [][] array = new int[m][n] ;
        for (int i=0 ; i<m ; i++)
            for(int j=0 ;j<n ;j++)
            {
                array[i][j]=reader.nextInt();
            }
        reader.close() ;
        // 对矩阵按行打出
        for (int i=0 ; i<m ; i++)
        {
            for(int j=0 ;j<n ;j++)
            {
                System.out.print(array[i][j]+" ");
            }
            System.out.println( );
        }            
    }
}

3. 输入一个单词字符串

import java.util.Scanner;
public class Class_3 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String c = in.next() ;
        System.out.println(c);
        in.close();
    }
}

4. 输入一个语句字符串

import java.util.Scanner;
public class Class_4 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine( ) ;
        System.out.println(str) ;
        in.close();
    }
}

5. 格式化输出

System.out.printf('%8.2f',float) ;

其他情况举例

情况一:不知道有多少行输入(或者多测试用例)。

Scanner sc = new Scanner(System.in);
ArrayList<String> arrayList = new ArrayList<>();
while (sc.hasNext()){
   arrayList.add(sc.next()); //nextLine()/next()看情况使用
}

情况二:输入为0结束输入

while ((num = sc.nextInt()) != 0){
.....
}

情况三:空行结束输入

while (true){
   String s = sc.nextLine();
   if(s.equals(""))
          break}

情况四:你知道有几行输入

Scanner in = new Scanner(System.in);
int n =in.nextInt();//n表示下面的输入行数
ArrayList<String> arr = new ArrayList<>();
While(n-- > 0){
  arr.add(in.next());
}
  • 4
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一些常用的函数和数据结构在LeetCode刷题中的应用示例: 1. 栈(Stack):栈是一种后进先出(LIFO)的数据结构,常用于解决与括号匹配、逆波兰表达式等问题。 ```java import java.util.Stack; Stack<Integer> stack = new Stack<>(); stack.push(1); // 入栈 stack.push(2); stack.push(3); int top = stack.peek(); // 获取栈顶元素,但不删除 int pop = stack.pop(); // 弹出栈顶元素 boolean isEmpty = stack.isEmpty(); // 判断栈是否为空 ``` 2. 队列(Queue):队列是一种先进先出(FIFO)的数据结构,常用于解决与广度优先搜索(BFS)相关的问题。 ```java import java.util.Queue; import java.util.LinkedList; Queue<Integer> queue = new LinkedList<>(); queue.offer(1); // 入队 queue.offer(2); queue.offer(3); int front = queue.peek(); // 获取队首元素,但不删除 int poll = queue.poll(); // 弹出队首元素 boolean isEmpty = queue.isEmpty(); // 判断队列是否为空 ``` 3. 堆(Heap):堆是一种特殊的树形数据结构,常用于解决与优先队列相关的问题。 ```java import java.util.PriorityQueue; PriorityQueue<Integer> minHeap = new PriorityQueue<>(); // 小顶堆 minHeap.offer(3); // 入堆 minHeap.offer(1); minHeap.offer(2); int min = minHeap.peek(); // 获取堆顶元素,但不删除 int pollMin = minHeap.poll(); // 弹出堆顶元素 boolean isEmpty = minHeap.isEmpty(); // 判断堆是否为空 ``` 4. 位运算(Bit Manipulation):位运算是对二进制数进行操作的技术,常用于解决与位操作相关的问题,如位与、位或、位异或等。 ```java int a = 5; // 二进制表示为 101 int b = 3; // 二进制表示为 011 int andResult = a & b; // 位与运算,结果为 001,即 1 int orResult = a | b; // 位或运算,结果为 111,即 7 int xorResult = a ^ b; // 位异或运算,结果为 110,即 6 int complement = ~a; // 取反运算,结果为 11111111111111111111111111111010,即 -6 int leftShift = a << 1; // 左移运算,结果为 1010,即 10 int rightShift = a >> 1; // 右移运算,结果为 10,即 2 ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值