郭郭的leetcode刷题笔记(队列和堆栈·一)

DAY1 队列和堆栈

232 用栈实现队列

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

错误一:只判断了self.q,应该self.q 和 self.z都判断


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

题解代码如下:用python3:

class MyQueue:

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

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

    def pop(self) -> int:
        """
        Removes the element from in front of queue and returns that element.
        """
        while len(self.q)>1:
            self.z.append(self.q.pop())
        ans = self.q.pop()
        
        while len(self.z)>0:
            self.q.append(self.z.pop())
        return ans


    def peek(self) -> int:
        """
        Get the front element.
        """
        while len(self.q)>1:
            self.z.append(self.q.pop())
        ans = self.q.pop()
        self.q.append(ans)
        while len(self.z)>0:
            self.q.append(self.z.pop())
        return ans



    def empty(self) -> bool:
        """
        Returns whether the queue is empty.
        """
        if len(self.q ) == 0 and len(self.z) == 0:
            return True
        else:
            return 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()

注意点:
1、list用append 和 pop
2、不能用empty 判断空

225 用队列实现栈

题目描述:使用队列实现栈的下列操作:

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

class MyStack:

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

    def push(self, x: int) -> None:
        """
        Push element x onto stack.
        """
        self.p.append(x)

    def pop(self) -> int:
        """
        Removes the element on top of the stack and returns that element.
        """
        while len(self.p)>1:
            self.q.append(self.p.pop(0))
        ans = self.p.pop()
        while not self.help_empty(self.q):
            self.p.append(self.q.pop(0))
        return ans
    def top(self) -> int:
        """
        Get the top element.
        """
        while len(self.p)>1:
            self.q.append(self.p.pop(0))
        ans = self.p.pop()
        self.q.append(ans)
        while not self.help_empty(self.q):
            self.p.append(self.q.pop(0))
        return ans
    def empty(self) -> bool:
        """
        Returns whether the stack is empty.
        """
        return len(self.p)==0
    def help_empty(self,x):
        return len(x)==0

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

注意:
1、pop(0) 和 popleft()含义相同
2、把辅助栈pop回原栈可以用self.help, self.data = self.data, self.help 代替,直接交换
3、self.p.pop()是pop最后一个元素,pop(0)才是第一个元素,例如【1,2,3,4】,pop()=4
4、不能用 while self.q is not self.empty,因为is的意思是判断左右是否相等,所以恒等,应该用while not self.help_empty(self.q):

155 最小的栈:

题目描述:设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈。

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

class MinStack:

    def __init__(self):
        """
        initialize your data structure here.
        """
        self.p,self.q=[],[]

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

    def pop(self) -> None:
        while len(self.p)>1:
            self.q.append(self.p.pop(0))
        ans = self.p.pop()
        self.p,self.q = self.q,self.p
        return ans
    def top(self) -> int:
        while len(self.p)>1:
            self.q.append(self.p.pop(0))
        ans = self.p.pop()
        self.q.append(ans)
        self.p,self.q = self.q,self.p
        return ans

    def getMin(self) -> int:
        ans = self.p[0]
        for x in self.p[1:]:
            ans = min(ans,x)
        return ans



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

本质上还是利用了用队列实现堆栈的思想。
注意:
1、getmin()函数中,一开始用了self.p.sort() return self.p[0]这样的想法,但是会原地更改p,使结果出错
2、sort()和sortted()的区别(要自己在pycharm里测试一下)
3、本质上并不是常数时间内,该如何改进?
4、sort()、sorted()的区别:
sorted()是内置函数,需要用
a=[3,2,1] sorted(a) print(a)
或者

a=[3,2,1]
a.sort()
 print(a)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值