首页 > 其他分享 >day10-栈与队列

day10-栈与队列

时间:2023-02-11 00:23:15浏览次数:70  
标签:return 队列 self pop day10 push empty

232. 用栈实现队列

leetcode题目:https://leetcode.cn/problems/implement-queue-using-stacks/description/

题目描述:请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty):
实现 MyQueue 类:

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

说明:

  • 你 只能 使用标准的栈操作 —— 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
  • 你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。

示例:

示例一:
输入: ["MyQueue", "push", "push", "peek", "pop", "empty"] [[], [1], [2], [], [], []]
输出: [null, null, null, 1, 1, false]
解释:
MyQueue myQueue = new MyQueue();
myQueue.push(1); // queue is: [1]
myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
myQueue.peek(); // return 1
myQueue.pop(); // return 1, queue is [2]
myQueue.empty(); // return false

题目解读&思考过程:

本题主要考察对队列和栈特性的理解,通过两个栈来实现队列的先进先出的特点;
自己第一次思考的过程其实很复杂,主要没有把两个栈的功能的界限划的很明显,即要存储元素又要弹出元素,这样就使得逻辑很复杂,所以需要简化即一个栈只负责入队时保存元素,另一个栈只负责出队时弹出元素。

Python代码:

class MyQueue:

    def __init__(self):
        self.stack_in = [] # 入栈
        self.stack_out = [] # 出栈

    def push(self, x: int) -> None:
        """ 入队操作:把元素加入入栈即可 """
        self.stack_in.append(x)

    def pop(self) -> int:
        """ 出队操作 """
        if self.empty(): # 先判断队列是否为空
            return None
        if self.stack_out: # 如果出栈不为空,移除出栈中的最后一个元素
            return self.stack_out.pop()
        else:
            self.stack_out = self.stack_in[::-1] # 如果出栈为空且队列不为空,则把入栈中的元素加入出栈
            self.stack_in = [] # 把入队清空
            return self.stack_out.pop()

    def peek(self) -> int:
        res = self.pop()
        self.stack_out.append(res)
        return res

    def empty(self) -> bool:
        return not (self.stack_in or self.stack_out)



# Your MyQueue object will be instantiated and called as such:
# obj = MyQueue()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.peek()
# param_4 = obj.empty()

225. 用队列实现栈

leetcode题目:https://leetcode.cn/problems/implement-stack-using-queues/

题目描述:请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(push、top、pop 和 empty)。
实现 MyStack 类:

  • void push(int x) 将元素 x 压入栈顶。
  • int pop() 移除并返回栈顶元素。
  • int top() 返回栈顶元素。
  • boolean empty() 如果栈是空的,返回 true ;否则,返回 false 。

注意:

  • 你只能使用队列的基本操作 —— 也就是 push to back、peek/pop from front、size 和 is empty 这些操作。
  • 你所使用的语言也许不支持队列。 你可以使用 list (列表)或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。

进阶:你能否仅用一个队列来实现栈。

示例:

输入: ["MyStack", "push", "push", "top", "pop", "empty"] [[], [1], [2], [], [], []]
输出: [null, null, null, 2, 2, false]
解释:
MyStack myStack = new MyStack();
myStack.push(1);
myStack.push(2);
myStack.top(); // 返回 2
myStack.pop(); // 返回 2
myStack.empty(); // 返回 False

题目解读&思考过程:

本题主要考察对队列和栈特性的理解,通过队列来实现栈先进后出的特点;
本题可以通过两个队列实现,也可以通过一个队列实现,思路都比较简单;下面通过Python的deque来完成。

Python代码:

from collections import deque
class MyStack:

    def __init__(self):
        self.queue = deque()

    def push(self, x: int) -> None:
        self.queue.append(x)

    def pop(self) -> int:
        if self.empty():
            return None
        for i in range(len(self.queue) - 1):
            self.queue.append(self.queue.popleft()) # 把先入对的弹出再重新加入
        return self.queue.popleft() # 再返回第一个元素

    def top(self) -> int:
        if self.empty():
            return None
        else:
            return self.queue[-1]

    def empty(self) -> bool:
        return not self.queue



# Your MyStack object will be instantiated and called as such:
# obj = MyStack()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.top()
# param_4 = obj.empty()

标签:return,队列,self,pop,day10,push,empty
From: https://www.cnblogs.com/zzggtt/p/17110732.html

相关文章