【小象算法Java版】第二节:栈和队列

预备知识

在这里插入图片描述
https://docs.oracle.com/javase/8/docs/api/

查看API,看看Stack的一些方法:
在这里插入图片描述
Queue 方法:

在这里插入图片描述

leetcode 225.用队列实现栈

题目描述

使用队列实现栈的下列操作:(传送门

push(x) – 元素 x 入栈
pop() – 移除栈顶元素
top() – 获取栈顶元素
empty() – 返回栈是否为空
注意:

你只能使用队列的基本操作-- 也就是 push to back, peek/pop from front, size, 和 is empty 这些操作是合法的。
你所使用的语言也许不支持队列。 你可以使用 list 或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。
你可以假设所有操作都是有效的(例如, 对一个空的栈不会调用 pop 或者 top 操作)。

解题思路&代码实现

使用两个队列,添加元素之前保证一个是空队列,queue1为空则向里边添加元素,然后循环将queue2元素添加到queue1中,这样保证元素都在一个栈中,并且最先进去的元素在栈顶,后进的在栈栈底。

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

class MyStack {
        Queue<Integer> queue1 = new LinkedList<>();

        Queue<Integer> queue2 = new LinkedList<>();

        /** Initialize your data structure here. */
        public MyStack() {

        }

        /** Push element x onto stack. */
        public void push(int x) {
            if (queue1.isEmpty()) {
                queue1.offer(x);
                while (!queue2.isEmpty()) {
                    queue1.offer(queue2.poll());
                }
            } else {
                queue2.offer(x);
                while (!queue1.isEmpty()) {
                    queue2.offer(queue1.poll());
                }
            }


        }

        /** Removes the element on top of the stack and returns that element. */
        public int pop() {
            if (empty()) {
                return 0;
            }
            if (queue2.isEmpty()) {
                return queue1.poll();
            } else {
                return queue2.poll();
            } 
        }

        /** Get the top element. */
        public int top() {
            if (empty()) {
                return 0;
            }
            if (queue2.isEmpty()) {
                return queue1.peek();
            } else {
                return queue2.peek();
            }

        }
        /**
         * Returns whether the stack is empty.
         */
        public boolean empty() {
            if (queue2.isEmpty() && queue1.isEmpty()) {
                return true;
            }
            return false;
        }
    }

leetcode 232.用栈实现队列

题目描述

请你仅使用两个栈实现先入先出队列。队列应当支持一般队列的支持的所有操作(push、pop、peek、empty):(传送门

实现 MyQueue 类:

void push(int x) 将元素 x 推到队列的末尾
int pop() 从队列的开头移除并返回元素
int peek() 返回队列开头的元素
boolean empty() 如果队列为空,返回 true ;否则,返回 false

解题思路&代码解析

思路是使用两个栈来实现队列,stack1用来进栈操作,stack2用来出栈操作。队列push直接往stack1入栈即可,出队列时stack2出栈即可,如果stack2为空则stack1出栈并添加到stack2.取队列元素也是一样。

class MyQueue {

    Stack<Integer> stack1 = new Stack<>();
    Stack<Integer> stack2 = new Stack<>();

    /** Initialize your data structure here. */
    public MyQueue() {

    }

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

    }

    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        if (!stack2.isEmpty()) {
            return stack2.pop();
        } else {
            while (!stack1.isEmpty()) {
                stack2.push(stack1.pop());
            }
        }
        return stack2.pop();

    }

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

    }

    /** Returns whether the queue is empty. */
    public boolean empty() {
        if (stack2.isEmpty() && stack1.isEmpty()) {
            return true;
        }
        return false;
    }
}

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue obj = new MyQueue();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.peek();
 * boolean param_4 = obj.empty();
 */

leetcode 155.最小栈

题目描述

设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈。(传送门

push(x) —— 将元素 x 推入栈中。
pop() —— 删除栈顶的元素。
top() —— 获取栈顶元素。
getMin() —— 检索栈中的最小元素。

示例:

输入:
["MinStack","push","push","push","getMin","pop","top","getMin"]
[[],[-2],[0],[-3],[],[],[],[]]

输出:
[null,null,null,null,-3,null,0,-2]

解释:
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin();   --> 返回 -3.
minStack.pop();
minStack.top();      --> 返回 0.
minStack.getMin();   --> 返回 -2.

解题思路&代码实现

维护一个最小栈stack1,添加元素是,stack1为空时之间添加进去,不为空时进行判断如果x比栈顶元素小则加入x否则在加入stack1栈顶元素。

在这里插入图片描述


class MinStack {
    Stack<Integer> stack1 = new Stack<>();
    Stack<Integer> stack = new Stack<>();


    /** initialize your data structure here. */
    public MinStack() {

    }

    public void push(int x) {
        stack.push(x);
        //Stack<Integer> stack2 = new Stack<>();

        if (stack1.isEmpty()) {
            stack1.push(x);
            return;
        }
        if (stack1.peek() > x) {
            stack1.push(x);
        } else {
            stack1.push(stack1.peek());
        }


    }

    public void pop() {
        stack.pop();
        stack1.pop();
    }

    public int top() {
        return  stack.peek();

    }

    public int getMin() {
        return stack1.peek();

    }
}


/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack obj = new MinStack();
 * obj.push(x);
 * obj.pop();
 * int param_3 = obj.top();
 * int param_4 = obj.getMin();
 */
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值