给定一个栈及一个操作序列int[][2] ope(C++中为vector<vector<int>>),代表所进行的入栈出栈操作。第一个元素为1则入栈,第二个元素为数的正负号;第一个元素为2则出栈,第二个元素若为0则出最先入栈的那个数,为1则出最先入栈的正数,为-1则出最先入栈的负数。请按顺序返回出栈的序列,并做异常处理忽略错误操作。
测试样例:
[[1,1],[1,-1],[2,0],[2,-1]]
返回:[1,-1]
给定一个栈及一个操作序列int[][2] ope(C++中为vector<vector<int>>),代表所进行的入栈出栈操作。第一个元素为1则入栈,第二个元素为数的正负号;第一个元素为2则出栈,第二个元素若为0则出最先入栈的那个数,为1则出最先入栈的正数,为-1则出最先入栈的负数。请按顺序返回出栈的序列,并做异常处理忽略错误操作。
[[1,1],[1,-1],[2,0],[2,-1]]
返回:[1,-1]
import java.util.*;
public class CatDogAsylum {
public ArrayList<Integer> asylum(int[][] ope) {
// write code here
ArrayList<Integer> res =new ArrayList<Integer>();
Deque<Integer> deque = new ArrayDeque<Integer>();
for(int i = 0;i<ope.length;i++){
if(ope[i][0]==1)deque.add(ope[i][1]);
if(ope[i][0]==2){
if(deque.isEmpty()) continue;
if(ope[i][1]==0){
res.add(deque.poll());
}
if(ope[i][1]==1){
if(deque.peekFirst()>0) res.add(deque.poll());
else{
Stack<Integer> stack = new Stack<Integer>();
while(!deque.isEmpty() && deque.peekFirst()<0){
stack.push(deque.pollFirst());
}
if(!deque.isEmpty()) res.add(deque.pollFirst());
while(!stack.isEmpty()){
deque.addFirst(stack.pop());
}
}
}
if(ope[i][1]==-1){
if(deque.peekFirst()<0) res.add(deque.poll());
else{
Stack<Integer> stack = new Stack<Integer>();
while( !deque.isEmpty() && deque.peekFirst()>0){
stack.push(deque.pollFirst());
}
if(!deque.isEmpty()) res.add(deque.pollFirst());
while(!stack.isEmpty()){
deque.addFirst(stack.pop());
}
}
}
}
}
return res;
}
} import java.util.*;
public class CatDogAsylum {
public static ArrayList<Integer> asylum(int[][] ope) {
// write code here
LinkedList<int[]> list = new LinkedList<>();
ArrayList<Integer> res = new ArrayList<>();
//遍历 二维数组的每一行
for (int[] item : ope) {
//比较 每一行的第一个元素 是否为 1,为1 就放入集合
if (item[0] == 1) {
list.add(item);
//比较 每一行的第一个元素 是否为21,为2 就出栈
} else if (item[0] == 2) {
//如果第二个元素 为 0,就删除
if (item[1] == 0) {
if (!list.isEmpty()) {
int[] animal = list.remove(0);
res.add(animal[1]);
}
} else {
for (int i = 0; i < list.size(); i++) {
//这个if 语句需要好好理解,它表示 输入数据的第二列 为 1 并且 入的 栈 的 第二个元素 大于 0,就移出当前元素
// 如果 输入数据的 第二列 为 -1,并且 入的栈 的第二个元素 小于 0,也移出,所以 判断条件不能少
if ((item[1] == 1 && list.get(i)[1] > 0) || (item[1] == -1 && list.get(i)[1] < 0)) {
int[] animal = list.remove(i);
res.add(animal[1]);
break;
}
}
}
}
}
return res;
}
} 求解惑...问题在哪
public ArrayList asylum(int[][] ope) {
// write code here
ArrayList list = new ArrayList();
ArrayList resList = new ArrayList();
for(int i=0;i<ope.length;i++){
if(ope[i][0]==1)
list.add(ope[i][1]);
if(ope[i][0]==2){
if(!list.isEmpty()){
if(ope[i][1]==0){
resList.add(list.get(0));
list.remove(0);
}
if(ope[i][1]==1){
int index = list.indexOf(1);
if(index!=-1){
resList.add(list.get(index));
list.remove(index);
}
}
if(ope[i][1]==-1){
int index = list.indexOf(-1);
if(index!=-1){
resList.add(list.get(index));
list.remove(index);
}
}
}
}
}
return resList;
} import java.util.ArrayList;
import java.util.LinkedList;
public class CatDogAsylum {
public ArrayList<Integer> asylum(int[][] ope) {
LinkedList<int[]> list = new LinkedList<>();
ArrayList<Integer> res = new ArrayList<>();
for (int[] items : ope) {
if (items[0] == 1) {
// 如果遇到宠物就加入列表中(收容所)
list.add(items);
} else if (items[0] == 2) {
// 如果遇到收养人
// 采取第一种收养方式
if (items[1] == 0) {
if (!list.isEmpty()) {
int[] animal = list.remove(0);
res.add(animal[1]);
}
} else {
// 收取猫或狗(若为1,则指定收养狗,若为-1则指定收养猫)
for (int i = 0; i < list.size(); i++) {
// 收容所有合适宠物则弹出
if ((items[1] == 1 && list.get(i)[1] > 0) || (items[1] == -1 && list.get(i)[1] < 0)) {
// 移除宠物
int[] animal = list.remove(i);
res.add(animal[1]);
// 结束本层循环
break;
}
}
}
}
}
return res;
}
}
import java.util.*;
public class CatDogAsylum {
public ArrayList<Integer> asylum(int[][] ope) {
// write code here
ArrayList<Integer> res = new ArrayList<Integer>();
Queue<Integer> animal = new LinkedList<Integer>();//尾部队列
Queue<Integer> head = new LinkedList<Integer>();//首部队列
for(int i = 0; i < ope.length; i++){
if(ope[i][0] == 1){
animal.offer(ope[i][1]);
}
if(ope[i][0] == 2){
if(ope[i][1] == 0){
if(!head.isEmpty()){
res.add(head.poll());
}else if(!animal.isEmpty()){
res.add(animal.poll());
}
}else{
if(!head.isEmpty()&&head.peek()*ope[i][1] > 0){
res.add(head.poll());
continue;
}
while(!animal.isEmpty()&&animal.peek()*ope[i][1] < 0){
head.offer(animal.poll());
}
if(!animal.isEmpty()){
res.add(animal.poll());
}
}
}
}
return res;
}
}
思路:只需要用一个额外的list就可以实现操作。
1 当为有动物进入收容所模式的时候,就直接用list存储对应的猫或狗.
(list集合add属性是先存储的数放在index索引最小处)。
2 当为有人收养动物的时候:
1) 直接收养最早的:即收养list中index索引最小的数;
2)领养最早进入收容所的狗:从list中最小的index索引遍历大于0的数(表示狗)并添加即可;
3)领养最早进入收容所的猫:从list中最小的index索引遍历小于0的数(表示猫)并添加即可;
code如下;
import java.util.*;
public class CatDogAsylum {
public static ArrayList<Integer> asylum(int[][] ope) {
ArrayList<Integer> list = new ArrayList<Integer>();
ArrayList<Integer> tempList = new ArrayList<Integer>();
int len = ope.length;//操作次数
for(int i = 0; i < len; i++){
if(ope[i][0] == 1){
//第二个数为0则为不合法操作,如(1,1)代表收养狗,(1,-4)代表收养猫;(1,0)无操作
if(ope[i][1] != 0){
tempList.add(ope[i][1]);
}
}
if(ope[i][0] == 2){
int tempListSize = tempList.size();
int index = 0;
//获取最早的收养动物
if(ope[i][1] == 0){
list.add(tempList.get(0));
tempList.remove(0);
}else if(ope[i][1] == 1){//获取收养最早的狗
while(index < tempListSize){
//tempList中放入最早的数即为index最小的数
int dog = tempList.get(index);
if(dog > 0){
list.add(dog);
tempList.remove(index);
break;
}
index++;
}
}else{//获取最早的猫
while(index < tempListSize){
int cat = tempList.get(index);
if(cat < 0){
list.add(cat);
tempList.remove(index);
break;
}
index++;
}
}
}
}
return list;
}
}
public:
vector<int> asylum(vector<vector<int> > ope) {
// write code here
vector<int> A; //定义动物序列
vector<int> B; //定义收养序列
int i; //定义循环变量
int len = ope.size();
for(i = 0; i < len; i ++)
{
if(ope[i][0] == 1) //有动物进来
A.push_back(ope[i][1]); //放入动物序列
else
if(A.empty())
continue;
else
if(ope[i][1] == 0) //第一种收养方式
{
B.push_back(A[0]);
A.erase(A.begin()); //从A中删除对应元素
}
else
if(ope[i][1] == 1) //收养狗
{
for(int j = 0; j < A.size(); j ++)
if(A[j] > 0)
{
B.push_back(A[j]);
A.erase(A.begin() + j); //从A中删除对应元素
break;
}
}
else
for(int j = 0; j < A.size(); j ++)
if(A[j] < 0)
{
B.push_back(A[j]);
A.erase(A.begin() + j); //从A中删除对应元素
break;
}
}
return B;
}
};