#-*- coding: UTF-8 -*-
class Stack(object):
def __init__(self):
"""
initialize your data structure here.
"""
self.inQueue=[]
self.outQueue=[]
def push(self, x):
"""
:type x: int
:rtype: nothing
"""
self.inQueue.append(x)
def pop(self):
"""
:rtype: nothing
"""
i=0
while i<len(self.inQueue)-1:
self.outQueue.append(self.inQueue[i])
i+=1
self.inQueue=self.outQueue
self.outQueue=[]
def top(self):
"""
:rtype: int
"""
tmpQueue=self.inQueue
i=0;
while i<(len(self.inQueue)-1):
self.outQueue.append(self.inQueue[i])
i+=1
res=[i for i in self.inQueue if i not in self.outQueue]
self.outQueue=[]
return res[0]
def empty(self):
"""
:rtype: bool
"""
return True if (len(self.inQueue))==0 else False
sol=Stack()
sol.push(1)
sol.push(2)
print sol.top()
sol.pop()
print sol.top()
sol.pop()
print sol.empty()
相关文章
- 03-11LeetCode 225. Implement Stack using Queues
- 03-11【leetcode❤python】 225. Implement Stack using Queues
- 03-11225. Implement Stack using Queues
- 03-11[LeetCode] 225. Implement Stack using Queues
- 03-11Leetcode 225 Implement Stack using Queues STL
- 03-11Java [Leetcode 225]Implement Stack using Queues
- 03-11LeetCode算法题-Implement Stack Using Queues
- 03-11(easy)LeetCode 225.Implement Stack using Queues
- 03-11【LeetCode题解】225_用队列实现栈(Implement-Stack-using-Queues)
- 03-11LeetCode(36)- Implement Stack using Queues