标签:
解答的思路:建立一个queue放狗,一个queue放猫。
如下:
import java.util.*; class Dog{ int time; int value; Dog(int a, int b){ time = a; value = b; } } class Cat{ int time; int value; Cat(int a, int b){ time = a; value = b; } } public class CatDogAsylum { public static ArrayList<Integer> asylum(int[][] ope){ ArrayList<Integer> res = new ArrayList(); Queue<Dog> dog = new ArrayDeque(); Queue<Cat> cat = new ArrayDeque(); int time = 0; for(int i = 0; i < ope.length; i++){ if(ope[i][0] == 1){//push if(ope[i][1] > 0){//dog Dog tmp = new Dog(time++,ope[i][1]); dog.add(tmp); } else if(ope[i][1] < 0){//cat Cat tmp = new Cat(time++,ope[i][1]); cat.add(tmp); } } else if(ope[i][0] == 2){//pop if(ope[i][1] == 0){ if(!dog.isEmpty() && ! cat.isEmpty()){ if(dog.peek().time < cat.peek().time){ res.add(dog.peek().value); dog.poll(); } else{ res.add(cat.peek().value); cat.poll(); } } else if(!dog.isEmpty()){ res.add(dog.peek().value); dog.poll(); } else { res.add(cat.peek().value); cat.poll(); } } else if(ope[i][1] == 1){ if(!dog.isEmpty()){ res.add(dog.peek().value); dog.poll(); } } else if(ope[i][1] == -1){ if(!cat.isEmpty()){ res.add(cat.peek().value); cat.poll(); } } } } return res; } }
标签:
原文地址:http://www.cnblogs.com/yueyebigdata/p/5065112.html