题解 | #第K小子串#

队列操作

http://www.nowcoder.com/questionTerminal/e542dfa97dd842578875fa592c982dbb

看到没人写JAVA,斗胆放上我的代码。
写的繁琐了点,欢迎提出改进意见。

import java.util.*;

public class Main{
     static class Queue{
        static class Qnode{
            Qnode next;
            Qnode prev;
            int value;
            public Qnode(){}
            public Qnode(int val){
                value = val;
            }
        }

        Qnode head;
        Qnode tail;
        int size;

        public Queue(){
            head = new Qnode();
            tail = new Qnode();
            head.next = tail;
            tail.prev = head;
            size = 0;
        }
        private int size(){
            return size;
        }

        private int peek(){
            return size == 0 ? -1 : head.next.value;
        }

        private int pop(){
            if(size == 0) return -1;
            Qnode temp = head.next;
            head.next = head.next.next;
            head.next.prev = head;
            size--;
            return temp.value;
        }

        private void clear(){
            head.next = tail;
            tail.prev = head;
            size = 0;
        }

        private void push(int val){
            Qnode newNode = new Qnode(val);
            newNode.prev = tail.prev;
            newNode.next = tail;
            newNode.prev.next = newNode;
            tail.prev = newNode;
            size++;
        }
    }

    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int groups = Integer.parseInt(sc.nextLine());
        for(int group = 0; group < groups; group++){
            Queue myQ = new Queue();
            myQ.clear();
            int operations = Integer.parseInt(sc.nextLine());
            for(int operation = 0; operation < operations; operation++){
                String opcode = sc.nextLine();
                int length = opcode.length();
                //if length >5 then it is an push operation
                if(length > 5){
                    String[] input = opcode.split(" ");
                    int num = Integer.parseInt(input[1]);
                    myQ.push(num);
                }
                //if it is a clear opearation
                if(opcode.equals("CLEAR")){
                    myQ.clear();
                }
                //if it is a TOP operation
                if(opcode.equals(("TOP"))){
                    System.out.println(myQ.peek());
                }
                //if it is a POP operation
                if(opcode.equals(("POP"))){
                    int pop = myQ.pop();
                    if(pop == -1) System.out.println(pop);
                }
                //if it is a SIZE operation
                if(opcode.equals(("SIZE"))){
                    System.out.println(myQ.size());
                }
            }
        }

    }
}
全部评论

相关推荐

07-02 13:50
闽江学院 Java
点赞 评论 收藏
分享
程序员小白条:你是沟通了900个,不是投了900份简历,你能投900份,意味着对面都要回复你900次,你早就找到实习了,没亮点就是这样的,别局限地区,时间投的也要早,现在都要7月了
点赞 评论 收藏
分享
06-15 02:05
已编辑
南昌航空大学 数据分析师
Eason三木:你如果想干技术岗,那几个发公众号合唱比赛的经历就去掉,优秀团员去掉,求职没用。然后CET4这种不是奖项,是技能,放到下面的专业技能里或者单独列一个英语能力。 另外好好改改你的排版,首行缩进完全没有必要,行间距好好调调,别让字和标题背景黏在一起,你下面说能做高质量PPT你得展现出来啊,你这简历排版我用PPT做的都能比你做的好。 然后自我评价,你如果要干数据工程师,抗压能力强最起码得有吧。
简历中的项目经历要怎么写
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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