华为OD机考题目:简易内存池

//    简易内存池
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();sc.nextLine();
        List<int[]> arrayList =new ArrayList<>();
        arrayList.add(new int[]{0,100,0});//0:首地址,  100:尾地址,   0|1(空闲|使用中)
        for (int j = 0; j < num; j++) {
            String[] str = sc.nextLine().split("=");
            int flag =0;

                if (str[0].equals("REQUEST")){
                    for (int i = 0; i < arrayList.size(); i++) {
                        int start = arrayList.get(i)[0],
                                end = arrayList.get(i)[1],
                                free = arrayList.get(i)[2];
                        int d = Integer.parseInt(str[1]);
                        if (d <= (end - start)&&free==0){
                            arrayList.remove(i);
                            arrayList.add(new int[]{start,start+d,1});
                            arrayList.add(new int[]{start+d,end,0});
                            //添加完后排序
                            arrayList.sort(Comparator.comparingInt(arr->arr[0]));
                            System.out.println(start);
                            flag=1;
                            break;
                        }
                    }
                    if (flag==0){
                        System.out.println("error");
                    }
                }

                else {
                    for (int i = 0; i < arrayList.size(); i++) {
                        int start = arrayList.get(i)[0],
                                end = arrayList.get(i)[1],
                                free = arrayList.get(i)[2];;
                        int d = Integer.parseInt(str[1]);
                        if (d == start&&free==1){
//                            判断左右有没有空闲内存
                            int left = 0,right=0;
                            int[] removeleft =new int[3],removeright = new int[3];
//                            第一个没有右空闲内存
                            if (i!=0&&arrayList.get(i-1)[2]==0){
                                removeleft = arrayList.remove(i - 1);
                                left=1;
                            }
//                            最后一个没有左空闲内存
                            if (i!=arrayList.size()-1&&arrayList.get(i+1)[2]==0){
                                removeright =  arrayList.remove(i + 1);
                                right=1;
                            }
                            arrayList.remove(i);
                            if (left==1&&right==1){
                                arrayList.add(new int[]{removeleft[0],removeright[1],0});
                            } else if (left==1&&right==0) {
                                arrayList.add(new int[]{removeleft[0],end,0});
                            } else if (left==0&&right==1) {
                                arrayList.add(new int[]{start,removeright[1],0});
                            }else {
                                arrayList.add(new int[]{start,end,0});
                            }
                            //添加完后排序
                            arrayList.sort(Comparator.comparingInt(arr->arr[0]));
                            flag=1;
                            break;
                        }
                    }
                    if (flag==0){
                        System.out.println("error");
                    }
                }
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值