需要自己定义def __ init__() 类的属性,然后push 操作直接放入self.stack1 当取出的时候,需要判断stack2中是否有元素 如果有直接取出 ,如果没有,则将self.stack1中元素放入self.stack2中再进行取出。
# -*- coding:utf-8 -*-
class Solution:
def __init__(self):
self.stack1=[]
self.stack2=[]
def push(self, node):
# write code here
self.stack1.append(node)
def pop(self):
if len(self.stack2)==0:
if len(self.stack1)==0:
return False
else:
while self.stack1:
self.stack2.append(self.stack1[-1])
self.stack1.pop()
result=self.stack2[-1]
self.stack2.pop()
return result
# return xx直接获取pop的值
# -*- coding:utf-8 -*-
class Solution:
def __init__(self):
self.stack1=[]
self.stack2=[]
def push(self, node):
# write code here
self.stack1.append(node)
def pop(self):
if len(self.stack2)==0:
if len(self.stack1)==0:
return False
else:
while self.stack1:
self.stack2.append(self.stack1.pop())
#self.stack1.pop()
#result=self.stack2[-1]
return self.stack2.pop()
# return xx
京公网安备 11010502036488号