话不多说,直接上代码(相信机智的你们肯定能看明白!):
# -*- coding: utf-8 -*-
# !usr/bin/env python
# 栈类(默认存储的元素都是整数)
class Stack():
# 初始化
def __init__(self):
self.__stack = []
self.__max2 = [] # 法二消耗的空间
self.__max3 = [] # 法三消耗的空间
# 入栈
def append(self, item):
self.__stack.append(item)
# 法二
if len(self.__max2) == 0:
self.__max2.append(item)
else:
if self.__max2[-1]>item:
self.__max2.append(self.__max2[-1])
else:
self.__max2