资源经验分享LeetCode初级算法--设计问题02:最小栈

LeetCode初级算法--设计问题02:最小栈

2019-11-09 | |  57 |   0

原标题:LeetCode初级算法--设计问题02:最小栈

原文来自:博客园      原文链接:https://www.cnblogs.com/aimi-cn/p/11749068.html


一、引子

这是由LeetCode官方推出的的经典面试题目清单~
这个模块对应的是探索的初级算法~旨在帮助入门算法。我们第一遍刷的是leetcode推荐的题目。
查看完整的剑指Offer算法题解析请点击github链接:
github地址

二、题目

设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈。

  • push(x) -- 将元素 x 推入栈中。

  • pop() -- 删除栈顶的元素。

  • top() -- 获取栈顶元素。

  • getMin() -- 检索栈中的最小元素。

示例:

MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin();   --> 返回 -3.minStack.pop();
minStack.top();      --> 返回 0.minStack.getMin();   --> 返回 -2.

1、思路

第一种方法:

用列表模拟栈,push、pop、top和getMin分别对应list.append()、list.pop()、list[-1]和min()操作

第二种方法:

引入minStack列表存放最小值

2、编程实现

第一种方法:

python

class MinStack(object):

    def __init__(self):
        """
        initialize your data structure here.
        """
        self.l = []        

    def push(self, x):
        """
        :type x: int
        :rtype: None
        """
        if x is None:            pass
        else:
            self.l.append(x)        

    def pop(self):
        """
        :rtype: None
        """
        if self.l is None:            return 'error'
        else:
            self.l.pop(-1)        

    def top(self):
        """
        :rtype: int
        """
        if self.l is None:            return 'error'
        else:            return self.l[-1]        

    def getMin(self):
        """
        :rtype: int
        """
        if self.l is None:            return 'error'
        else:            return min(self.l)# Your MinStack object will be instantiated and called as such:# obj = MinStack()# obj.push(x)# obj.pop()# param_3 = obj.top()# param_4 = obj.getMin()

第二种方法:

class MinStack(object):
 
    def __init__(self):
        """
        initialize your data structure here.
        """
        self.stack = []       #存放所有元素
        self.minStack = []#存放每一次压入数据时,栈中的最小值(如果压入数据的值大于栈中的最小值就不需要重复压入最小值,小于或者等于栈中最小值则需要压入)
 
    def push(self, x):
        """
        :type x: int
        :rtype: void
        """
        self.stack.append(x)        if not self.minStack or self.minStack[-1]>=x:
            self.minStack.append(x) 
    def pop(self):   #移除栈顶元素时,判断是否移除栈中最小值
        """
        :rtype: void
        """
        if self.minStack[-1]==self.stack[-1]:            del self.minStack[-1]
        self.stack.pop() 
    def top(self):
        """
        :rtype: int
        """
        return self.stack[-1]        
    def getMin(self):
        """
        :rtype: int
        """
        return self.minStack[-1]


免责声明:本文来自互联网新闻客户端自媒体,不代表本网的观点和立场。

合作及投稿邮箱:E-mail:editor@tusaishared.com

上一篇:基于Keras搭建MLP

下一篇:LeetCode初级算法--其他01:位1的个数

用户评价
全部评价

热门资源

  • Python 爬虫(二)...

    所谓爬虫就是模拟客户端发送网络请求,获取网络响...

  • TensorFlow从1到2...

    原文第四篇中,我们介绍了官方的入门案例MNIST,功...

  • TensorFlow从1到2...

    “回归”这个词,既是Regression算法的名称,也代表...

  • 机器学习中的熵、...

    熵 (entropy) 这一词最初来源于热力学。1948年,克...

  • TensorFlow2.0(10...

    前面的博客中我们说过,在加载数据和预处理数据时...