0
点赞
收藏
分享

微信扫一扫

牛客网 CM7 :分条件出栈

婉殇成长笔记 2022-03-11 阅读 16
import java.util.*;

public class CatDogAsylum {
    public void func(int[] arr,int head,int y) {
        if (y==0) {
            return;
        }
        for (int i = y; i >head ; i--) {
            arr[i] = arr[i-1];
        }
    }
    public ArrayList<Integer> asylum(int[][] ope) {
        if(ope ==null||ope.length==0) {
            return null;
        }
        ArrayList<Integer> arrayList = new ArrayList<>();
        int[] elem = new int[ope.length];
        int head = 0;
        int tail = 0;

        for(int i =0;i<ope.length;i++) {
            if(ope[i][0]==1) {
                //add
                elem[tail++] = ope[i][1];
            } else if(ope[i][0]==2) {
                //删除
                if (tail == 0) {
                    
                }

                if(ope[i][1] == 0) {
                    int ret = elem[head];
                    arrayList.add(ret);
                    head++;
                } else if (ope[i][1] == 1) {

                    int j = 0;
                    for (j =head; j < tail; j++) {
                        if(elem[j]>0) {
                            int ret = elem[j];
                            arrayList.add(ret);
                            //  从  head  将其覆盖
                            func(elem,head,j);
                            head++;
                            break;
                        }
                    }
                    if(j==tail) {
                       
                    }
                } else if (ope[i][1]==-1) {

                    int j = 0;
                    for (j =head; j < tail; j++) {
                        if(elem[j] < 0) {
                            int ret = elem[j];
                            arrayList.add(ret);
                            //  从  head  将其覆盖
                            func(elem,head,j);
                            head++;
                            break;
                        }
                    }
                    if(j==tail) {
                       
                    }
                }
            }
        }
        // write code here
        return arrayList;
    }
}

在这里插入图片描述

举报

相关推荐

0 条评论