题解 | 【模板】序列操作

【模板】序列操作

https://www.nowcoder.com/practice/12da4185c0bb45918cfdc3072e544069

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    static List<Integer> nums=new ArrayList<>();
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int q=in.nextInt();
        for(int i=0;i<q;i++){
            int first = in.nextInt();
            int second = 0;
            int thrid = 0;
            if(first==1 || first==3){
                second = in.nextInt();
            }else if(first==4){
                second = in.nextInt();
                thrid = in.nextInt();
            }
            make(first,second,thrid);
        }
    }
    public static void make(int first,int second,int thrid){
        switch(first){
            case 1 :
                nums.add(second);
                break;
            case 2 :
                nums.remove(nums.size()-1);
                break;
            case 3 :
                System.out.println(nums.get(second));
                break;
            case 4 :
                nums.add(second+1,thrid);
                break;
            case 5 : 
                nums.sort((a,b)->a-b);
                break;
            case 6 : 
                nums.sort((a,b)->b-a);
                break;
            case 7 :
                System.out.println(nums.size());
                break;
            case 8 : 
                for(int i = 0;i<nums.size();i++){
                    System.out.print(nums.get(i)+" ");
                }
                System.out.println("");
                break;
        }
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务