当前位置:网站首页>[Jianzhi offer] Jianzhi offer 09 Implementing queues with two stacks

[Jianzhi offer] Jianzhi offer 09 Implementing queues with two stacks

2022-06-12 21:52:00 Caicai 2022

subject
Code
Execution time :364 ms, In all Python3 Defeated in submission 39.23% Users of
Memory consumption :19 MB, In all Python3 Defeated in submission 24.23% Users of
Pass the test case :55 / 55

class CQueue:

    def __init__(self):
        self.stack1=[]
        self.stack2=[]


    def appendTail(self, value: int) -> None:
        self.stack1.append(value)

    def deleteHead(self) -> int:
        if not len(self.stack1) and not len(self.stack2):
            return -1
        else:
            if len(self.stack2):
                temp=self.stack2[-1]
                self.stack2.pop()
                return temp
            else:
                while len(self.stack1):
                    temp=self.stack1[-1]
                    self.stack1.pop()
                    self.stack2.append(temp)
                self.stack2.pop()
                return temp
# Your CQueue object will be instantiated and called as such:
# obj = CQueue()
# obj.appendTail(value)
# param_2 = obj.deleteHead()
原网站

版权声明
本文为[Caicai 2022]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202281204317887.html