猫狗队列

原题链接
猫狗队列_第1张图片

着重处理 pollAll 即可,其他的都好说

import java.util.*;
import java.io.*;

public class Main {
    static int n;
    static Queue<Dog> dogs = new LinkedList<>();
    static Queue<Cat> cats = new LinkedList<>();
    static int cnt;//计数器
    
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        n = Integer.parseInt(br.readLine());
        while (n-- > 0) {
            String[] line = br.readLine().split(" ");
            String op = line[0];
            if (op.equals("add")) {
                String type = line[1];
                int id = Integer.parseInt(line[2]);
                if (type.equals("cat")) {
                    Cat c = new Cat(id);
                    cats.add(c);
                    cnt++;//入队后,计数器++
                    c.count = cnt;//给这只猫盖上时间戳
                } else {
                    Dog d = new Dog(id);
                    dogs.add(d);
                    cnt++;
                    d.count = cnt;//给这只狗盖上时间戳
                }
            } else if (op.equals("isEmpty")) {
                boolean flag = isEmpty();
                if (flag) {
                    System.out.println("yes");
                } else {
                    System.out.println("no");
                }
            } else if (op.equals("isDogEmpty")) {
                if (isDogEmpty()) {
                    System.out.println("yes");
                } else {
                    System.out.println("no");
                }
            } else if (op.equals("isCatEmpty")) {
                if (isCatEmpty()) {
                    System.out.println("yes");
                } else {
                    System.out.println("no");
                }
            } else if (op.equals("pollDog")) {
                pollDog();
            } else if (op.equals("pollCat")) {
                pollCat();
            } else {
                pollAll();
            }
        }
    }
    
    private static boolean isEmpty() {
        return dogs.isEmpty() && cats.isEmpty();
    }
    
    private static boolean isDogEmpty() {
        return dogs.isEmpty();
    }
    
    private static boolean isCatEmpty() {
        return cats.isEmpty();
    }
    
    private static void pollDog() {
        while (!dogs.isEmpty()) {
            System.out.println("dog " + dogs.poll().id);
        }
    }
    
    private static void pollCat() {
        while (!cats.isEmpty()) {
            System.out.println("cat " + cats.poll().id);
        }
    }
    
    private static void pollAll() {
        while (!cats.isEmpty() && !dogs.isEmpty()) {
            if (dogs.peek().count < cats.peek().count) {//输出时间戳小的
                System.out.println("dog " + dogs.poll().id);
            } else {
                System.out.println("cat " + cats.poll().id);
            }
        }
        while (!cats.isEmpty()) {
            System.out.println("cat " + cats.poll().id);
        }
        while (!dogs.isEmpty()) {
            System.out.println("dog " + dogs.poll().id);
        }
    }
}

class Dog {
    int id;
    int count;//狗的计数器
    
    public Dog(int id) {
        this.id = id;
    }
}

class Cat {
    int id;
    int count;//猫的计数器
    
    public Cat(int id) {
        this.id = id;
    }
}

你可能感兴趣的:(LeetCode)