To prove safety net offer cattle off exercise 20,200,127

1. The stack includes the min function

The beginning he did not understand the idea, to see the forum is meant to be two stacks, then I wanted to understand

Use more than a small stack storage value

A multi-stack push conditions: the stack is empty or less stack elements

Condition the stack: the stack is not empty and the stack of elements equal to

# -*- coding: utf-8 -*-
"""
Created on Sun Dec 22 14:41:47 2019

@author: Shiyi Chen
"""
# -*- coding:utf-8 -*-
class Solution:
    def __init__(self):
        self.arr=[]
        self.mini=[]
    def push(self, node):
        # write code here
        self.arr.append(node)
        if self.mini:
            mix=self.mini[-1]
            if node<=mix:
                self.mini.append(node)
        else:
            self.mini.append(node)
    def pop(self):
        # write code here
        if self.arr:
            if self.arr[-1]==self.mini[-1]:
                self.mini.pop()
            return self.arr.pop()
        else:
            return None
    def top(self):
        # write code here
        if self.arr:
            return self.arr[-1]
        else:
            return None
    def min(self):
        # write code here
        if self.mini:
            return self.mini[-1]
        else:
            return None

 

Published 39 original articles · won praise 6 · views 20000 +

Guess you like

Origin blog.csdn.net/XinemaChen/article/details/104094657