Leetcode算法题-栈

面试题 03.01. 三合一

https://leetcode-cn.com/problems/three-in-one-lcci/

public class TripleInOne {

    //定义一个数组栈
    private int[] stack;
    //定义一个头指针的数组
    private int[] top;

    public TripleInOne(int stackSize) {
        //定义三栈合一
       stack = new int[stackSize * 3];
       // 三个栈的头结点
       top = new int[3];

       //分别赋值第一次的头结点
        // 栈0 top[0] = 0;
        // 栈1 top[1] = 1;
        // 栈2 top[2] = 2;
       for (int i = 0; i < 3; i++) {
           top[i] = i;
       }
    }

    public void push(int stackNum, int value) {
        //判断是否添加满了
        if (top[stackNum] < stack.length) {
            //赋值
            stack[top[stackNum]] = value;
            //头结点+3
            top[stackNum] = top[stackNum] + 3;
        }
    }

    public int pop(int stackNum) {

        //判断是否为空
        if (isEmpty(stackNum)) {
            return -1;
        }
        //原本的top[stackNum]减3
        top[stackNum] = top[stackNum] - 3;

        //返回
        return stack[(top[stackNum])];


    }

    public int peek(int stackNum) {
        //判断是否为空
        if (isEmpty(stackNum)) {
            return -1;
        }
        //把下标给它但是不改变原来的top[stackNum]
        int index = top[stackNum] - 3;
        return stack[index];
    }

    public boolean isEmpty(int stackNum) {
        //判断这个栈是否为空
        if (top[stackNum] -3 < 0) {
            return true;
        }
        return false;
    }
}

面试题 03.02. 栈的最小值

https://leetcode-cn.com/problems/min-stack-lcci/

class MinStack {
    
    Stack<Integer> stackA;
    Stack<Integer> stackB;

    /** initialize your data structure here. */
    public MinStack() {
        stackA = new Stack<>();
        stackB = new Stack<>();
    }

    public void push(int x) {
        stackA.push(x);
        if (stackB.isEmpty() || stackB.peek() >= x) stackB.push(x);
    }

    public void pop() {
        int x = stackA.pop();
        if (stackB.peek() == x) stackB.pop();
    }

    public int top() {
        return stackA.peek();
    }

    public int getMin() {
        return stackB.peek();
    }
}

面试题 03.03. 堆盘子

https://leetcode-cn.com/problems/stack-of-plates-lcci/

class StackOfPlates{
    private List<Stack<Integer>> stacklist;
    private int cap;
    
    public StackOfPlates(int cap) {
        stacklist = new ArrayList<>();
        this.cap = cap;
    }
    
    public void push(int val) {
        if (cap <= 0) return;
        if (stacklist.isEmpty() || stacklist.get(stacklist.size() - 1).size() == cap) {
            Stack<Integer> stack = new Stack<>();
            stack.push(val);
            stacklist.add(stack);
        } else {
            stacklist.get(stacklist.size() - 1).push(val);
        }
    }
    
    public int pop() {
        return popAt(stacklist.size() - 1);
    }
    
    public int popAt(int index) {
        if (index < 0 || index >= stacklist.size()) return -1;
        Stack<Integer> stack = stacklist.get(index);
        if (stack.isEmpty()) return -1;
        int res = stack.pop();
        if (stack.isEmpty()) {
            stacklist.remove(index);
        }
        return res;
    }
}

面试题 03.04. 化栈为队

https://leetcode-cn.com/problems/implement-queue-using-stacks-lcci/

class MyQueue {

    Stack<Integer> numstack;
    Stack<Integer> helpstack;
    /** Initialize your data structure here. */
    public MyQueue() {
        numstack = new Stack<>();
        helpstack = new Stack<>();
    }

    /** Push element x to the back of queue. */
    public void push(int x) {
        numstack.push(x);
    }

    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        peek();
        return helpstack.pop();
    }

    /** Get the front element. */
    public int peek() {
        if (helpstack.isEmpty()) {
            while (!numstack.isEmpty()) {
                helpstack.push(numstack.pop());
            }
        }
        return helpstack.peek();
    }

    /** Returns whether the queue is empty. */
    public boolean empty() {
        return numstack.isEmpty() && helpstack.isEmpty();
    }
}

面试题 03.05. 栈排序

https://leetcode-cn.com/problems/sort-of-stacks-lcci/

class SortedStack {
    //原始栈
    Deque<Integer> stack = new LinkedList<>();
    //辅助栈
    Deque<Integer> tmp = new LinkedList<>();
    public SortedStack() {

    }
    
    public void push(int val) {
        int max = stack.isEmpty() ? Integer.MAX_VALUE : stack.peek();
        int min = tmp.isEmpty() ? Integer.MIN_VALUE : tmp.peek();
        //比较原始栈与辅助栈栈顶值,使其满足 辅助栈 <= val <= 原始栈
        while(true){
            if(val > max){
                tmp.push(stack.pop());
                max = stack.isEmpty() ? Integer.MAX_VALUE : stack.peek();
            }
            else if(val < min){
                stack.push(tmp.pop());
                min = tmp.isEmpty() ? Integer.MIN_VALUE : tmp.peek();
            }
            else{
                stack.push(val);
                break;
            }
        }
    }
    
    public void pop() {
        //将辅助栈元素push回原始栈
        while (!tmp.isEmpty()){
            stack.push(tmp.pop());
        }
        if (!stack.isEmpty())
            stack.pop();
    }
    
    public int peek() {
        //将辅助栈元素push回原始栈
        while (!tmp.isEmpty()){
            stack.push(tmp.pop());
        }
        return stack.isEmpty() ? -1 : stack.peek();
    }
    
    public boolean isEmpty() {
        return stack.isEmpty() && tmp.isEmpty();
    }
}

面试题 03.06. 动物收容所

https://leetcode-cn.com/problems/animal-shelter-lcci/

class AnimalShelf {
    Queue<int[]> queue;

    public AnimalShelf() {
        queue = new LinkedList<>();
    }
    
    public void enqueue(int[] animal) {
        queue.offer(animal);
    }
    
    public int[] dequeueAny() {
        if(queue.isEmpty())
            return new int[]{-1,-1};
        return queue.poll();
    }
    
    public int[] dequeueDog() {
        for(int[] animal : queue)
            if(animal[1] == 1){
                queue.remove(animal);
                return animal;
            }
        return new int[]{-1, -1};
    }
    
    public int[] dequeueCat() {
        for(int[] animal : queue)
            if(animal[1] == 0){
                queue.remove(animal);
                return animal;
            }
        return new int[]{-1, -1};
    }
}

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值