题解 | #【模板】队列#

【模板】队列

http://www.nowcoder.com/practice/afe812c80ad946f4b292a26dd13ba549

import java.util.Scanner;

class MyQueue{
    private int size;
    private int t1=0, t2=0;
    private int[] q;
    public MyQueue(int s){
        this.size = s;
        this.q = new int[s];
    }
    public void push(int x){
        if(this.t2 == this.size-1){
            System.out.println("error");
        } else {
            this.q[t2++] = x;
        }
    }
    public void pop(){
        if(t1>=t2) System.out.println("error");
        else {
            System.out.println(this.q[t1++]);
        }
    }
    
    public void front(){
        if(t1>=t2) System.out.println("error");
        else {
            System.out.println(this.q[t1]);
        }
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int s = Integer.parseInt(in.nextLine());
        MyQueue q = new MyQueue(s);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String[] arr = in.nextLine().split(" ");
            switch(arr[0]){
                case "push": q.push(Integer.parseInt(arr[1]));break;
                case "pop": q.pop();break;
                case "front": q.front();break;
                default: break;
            }
        }
    }
}
全部评论

相关推荐

昨天 18:59
已编辑
绍兴文理学院 Java
一只末影酱:一、1w+qps嘛感觉数据有点太夸张了 二、还有就是99.95%这些,本身大部分学生做的小项目基本是100%,因为量太小了,网络抖动问题也基本模拟不出来,感觉这些不太好写 三、你这些项目,都是一个月就做完了,更抽象了,也就是大概意味着,没有技术调研,没有上线测试,
点赞 评论 收藏
分享
半解316:内容充实,细节需要修改一下。 1,整体压缩为一页。所有内容顶格。 2,项目描述删除,直接写个人工作量 修改完之后还需要建议,可以私聊
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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