leetcode python3 简单题232. Implement Queue using Stacks

1.编辑器

我使用的是win10+vscode+leetcode+python3
环境配置参见我的博客:
链接

2.第二百三十二题

(1)题目
英文:
Implement the following operations of a queue using stacks.

push(x) – Push element x to the back of queue.
pop() – Removes the element from in front of queue.
peek() – Get the front element.
empty() – Return whether the queue is empty.
Example:

MyQueue queue = new MyQueue();

queue.push(1);
queue.push(2);
queue.peek(); // returns 1
queue.pop(); // returns 1
queue.empty(); // returns false

中文:
使用栈实现队列的下列操作:

push(x) – 将一个元素放入队列的尾部。
pop() – 从队列首部移除元素。
peek() – 返回队列首部的元素。
empty() – 返回队列是否为空。
示例:

MyQueue queue = new MyQueue();

queue.push(1);
queue.push(2);
queue.peek(); // 返回 1
queue.pop(); // 返回 1
queue.empty(); // 返回 false

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/implement-queue-using-stacks

(2)解法
全部用库
(耗时:32ms,内存:13.5M)

class MyQueue:

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.queue=[]

    def push(self, x: int) -> None:
        """
        Push element x to the back of queue.
        """
        self.queue.append(x)

    def pop(self) -> int:
        """
        Removes the element from in front of queue and returns that element.
        """
        return self.queue.pop(0)

    def peek(self) -> int:
        """
        Get the front element.
        """

        return self.queue[0]

    def empty(self) -> bool:
        """
        Returns whether the queue is empty.
        """
        return not bool(self.queue)



# 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()

注意:
1.deque中的pop是不能有索引的,必须是pop最后边的元素;而list中的pop是可以选择索引的。

② 原始方法
(耗时:44ms,内存:13.5M)

class MyQueue:

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.queue=[]

    def push(self, x: int) -> None:
        """
        Push element x to the back of queue.
        """
        self.queue.append(x)

    def pop(self) -> int:
        """
        Removes the element from in front of queue and returns that element.
        """
        buff=[]
        while len(self.queue)>0:
            buff.append(self.queue[-1])
            del self.queue[-1]
        num=buff[-1]
        del buff[-1]
        while len(buff)>0:
            self.queue.append(buff[-1])
            del buff[-1]
        return num

    def peek(self) -> int:
        """
        Get the front element.
        """
        buff=[]
        length=len(self.queue)
        while length>0:
            buff.append(self.queue[length-1])
            length-=1
        return buff[-1]

    def empty(self) -> bool:
        """
        Returns whether the queue is empty.
        """
        return True if len(self.queue)==0 else False



# 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()

猜你喜欢

转载自blog.csdn.net/qq_37285386/article/details/106108627