栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【坚持每日一题10.16】三合一

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【坚持每日一题10.16】三合一

三合一。描述如何只用一个数组来实现三个栈。

你应该实现push(stackNum, value)、pop(stackNum)、isEmpty(stackNum)、peek(stackNum)方法。stackNum表示栈下标,value表示压入的值。

构造函数会传入一个stackSize参数,代表每个栈的大小。

示例1:

输入:
[“TripleInOne”, “push”, “push”, “pop”, “pop”, “pop”, “isEmpty”]
[[1], [0, 1], [0, 2], [0], [0], [0], [0]]
输出:
[null, null, null, 1, -1, -1, true]
说明:当栈为空时pop, peek返回-1,当栈满时push不压入元素。
示例2:

输入:
[“TripleInOne”, “push”, “push”, “push”, “pop”, “pop”, “pop”, “peek”]
[[2], [0, 1], [0, 2], [0, 3], [0], [0], [0], [0]]
输出:
[null, null, null, null, 2, 1, -1, -1]

java代码:
class TripleInOne{
    public int[] a;
    private int stackSize;
    private int index1,index2,index3;
    public TripleInOne(int stackSize) {
        this.stackSize = stackSize;
        a = new int[stackSize * 3];
        index1 = -1;// [0,stackSize)
        index2 = stackSize - 1;//[stackSize,stackSize*2)
        index3 = stackSize * 2 - 1 ;//[stackSize*2,stackSize*3)
        for (int i = 0; i < a.length; i++) {
            a[i] = -1;
        }
    }

    public void push(int stackNum, int value) {
        if (stackNum == 0){
            if (index1+1 < stackSize){
                index1++;
                a[index1] = value;
            }else{
                return;
            }
        }
        if (stackNum == 1){
            if (index2+1 < stackSize*2){
                index2++;
                a[index2] = value;
            }else{
                return;
            }
        }
        if (stackNum == 2){
            if (index3+1 < stackSize*3){
                index3++;
                a[index3] = value;
            }
        }
    }


    public int pop(int stackNum) {
        if (stackNum == 0 && index1 >= 0){
                index1--;
                return a[index1+1];
        }
        if (stackNum == 1 && index2 >= stackSize){
                index2--;
                return a[index2+1];
        }
        if (stackNum == 2 && index3 >= stackSize*2){
                index3--;
                return a[index3+1];
        }
        return -1;
    }

    public int peek(int stackNum) {
        int result = -1;
        if (stackNum == 0 && index1 >= 0){
                result = a[index1];
        }
        if (stackNum == 1 && index2 >= stackSize){
                result = a[index2];
        }
        if (stackNum == 2 && index3 >= stackSize*2){
                result = a[index3];
        }
        return result;
    }

    public boolean isEmpty(int stackNum) {
        if (stackNum == 0 && index1 == -1){
                return true;
        }
        if (stackNum == 1 && index2 == stackSize-1){
                return true;
        }
        if (stackNum == 2 && index3 == stackSize*2-1){
                return true;
        }
        return false;
    }
}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/327532.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号