https://leetcode.com/problems/min-stack/#/solutions

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.

  • push(x) -- Push element x onto stack.
  • pop() -- Removes the element on top of the stack.
  • top() -- Get the top element.
  • getMin() -- Retrieve the minimum element in the stack.

Example:

MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); --> Returns -3.
minStack.pop();
minStack.top(); --> Returns 0.
minStack.getMin(); --> Returns -2.
 
Sol:
 
 AC
 
class MinStack(object):

    def __init__(self):
"""
initialize your data structure here.
"""
self.stack = []
# set two stacks
self.minStack = [] def push(self, x):
self.stack.append(x)
if len(self.minStack) and x == self.minStack[-1][0]:
self.minStack[-1] = (x, self.minStack[-1][1] + 1)
elif len(self.minStack) == 0 or x < self.minStack[-1][0]:
self.minStack.append((x, 1)) def pop(self):
#如果 栈顶值 == 最小值栈顶值
if self.top() == self.getMin():
#如果 最小值栈顶元素次数 > 1
if self.minStack[-1][1] > 1:
#最小值栈顶元素次数 - 1
self.minStack[-1] = (self.minStack[-1][0], self.minStack[-1][1] - 1)
else:
#最小值栈顶元素弹出
self.minStack.pop()
return self.stack.pop() def top(self):
return self.stack[-1] def getMin(self):
return self.minStack[-1][0] # 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()
 

155. Min Stack - Unsolved的更多相关文章

  1. leetcode 155. Min Stack 、232. Implement Queue using Stacks 、225. Implement Stack using Queues

    155. Min Stack class MinStack { public: /** initialize your data structure here. */ MinStack() { } v ...

  2. leetcode 155. Min Stack --------- java

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  3. Java [Leetcode 155]Min Stack

    题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant ti ...

  4. 155. Min Stack

    题目: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time ...

  5. Java for LeetCode 155 Min Stack

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  6. 【leetcode】155 - Min Stack

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  7. LeetCode题解 #155 Min Stack

    写一个栈,支持push pop top getMin 难就难在在要在常量时间内返回最小的元素. 一开始乱想了很多东西,想到了HashMap,treeMap,堆什么的,都被自己一一否决了. 后来想到其实 ...

  8. [LeetCode] 155. Min Stack 最小栈

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  9. LC 155 Min Stack

    问题描述 Design a stack that supports push, pop, top, and retrieving the minimum element in constant tim ...

随机推荐

  1. linux下的C++项目创建

    CMake项目的完整构建 Linux下的CMake项目通常由几个文件夹组成.小伙伴们可以先在自己的电脑上新建一个文件夹,作为你代码的根目录,然后往里面建几个子文件夹,这里并不涉及具体的代码,只是可以作 ...

  2. TOJ 2755 国际象棋(搜索)

    传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=2755 思路:对起点到终点进行广搜, ...

  3. Angular之RouterModule的forRoot与forChild

    Angular 提供了一种方式来把服务提供商从模块中分离出来,以便模块既可以带着 providers 被根模块导入,也可以不带 providers 被子模块导入. 区别: `forRoot` crea ...

  4. CodeForces - 55D(数位dp,离散化)

    题目来源:http://codeforces.com/problemset/problem/55/D Volodya is an odd boy and his taste is strange as ...

  5. scrapy的调试和环境安装技巧

    1,先在settings中把ROBOTSTXT_OBEY = False 在主目录下面新建main文件 __autor__ = 'zhouli' __date__ = '2018/11/3 22:39 ...

  6. swift语言版本选择 - 解决XCode报错:The “Swift Language Version” (SWIFT_VERSION) build setting must be set to a supported valu

    转发链接:https://blog.csdn.net/nathan1987_/article/details/79757368 The “Swift Language Version” (SWIFT_ ...

  7. shelve

    shelve是对pickle的封装 json & pickle是把所有的数据全部封装,一次性写入文件,而shelve可以把数据分类,以键值对的形式分别写入文件 shelve模块是一个简单的k, ...

  8. linux虚拟机ping不通主机和外网(包括刚装系统遇到的一些问题)

    自己ubuntu系统安装了一个virtualBox虚拟机,里面又装了一个ubuntu-server系统: 1.先设置一下字符集,因为一开始装系统的时候选择的是中文,但里面始终无法支持中文,那就算了,反 ...

  9. 线特征---LSD and LBD程序运行(一)

    最近在看有关特征提取的线特征,暑期就看了相关的论文:<基于点线综合特征的双目视觉SLAM方法_谢晓佳>,最近呢,把里面有关线特征提取LSD和描述子LBD的代码跑了一遍,记录如下: [1]L ...

  10. stark组件开发之URL别名的设置

    from django.urls import re_path from stark.servers.start_v1 import site, StartHandler from django.ht ...