Min Stack [LeetCode 155]
1- 问题描述
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
2- 思路分析
使用两个栈,一个栈保存原始数据(stack),另一个栈保持每个元素对应的最小元素(minStack)。如stack中从栈底到栈顶为[3, 1, 5, 7, 0],那么minStack中从栈底到栈顶为[3, 1, 1, 1, 0]。可参考[LeetCode] Min Stack in Python
3- Python实现
# -*- coding: utf-8 -*-
# Min Stack class MinStack:
def __init__(self):
self.stack = []
self.min = [] # 保存最小值 # @param x, an integer
# @return an integer
def push(self, x): # 入栈
self.stack.append(x)
if not self.min: # 如果min栈为空则直接入栈
self.min.append(x)
return x
if x < self.min[-1]: # 如果入栈元素x比min栈顶小,则x入栈
self.min.append(x)
else: # 如果x比min栈顶大,则重复栈顶元素
self.min.append(self.min[-1])
return x # @return nothing
def pop(self): #出栈
self.stack.pop()
self.min.pop() # min栈同步 # @return an integer
def top(self): #栈顶
return self.stack[-1] # @return an integer
def getMin(self): #栈内最小值
return self.min[-1]
Min Stack [LeetCode 155]的更多相关文章
- Min Stack leetcode
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- Min Stack (LeetCode) tweak it to avoid Memory Limit Exceeded
class MinStack { public: void push(int x) { if(values.empty()) { values.push_back(x); min_indices.pu ...
- LeetCode 155:最小栈 Min Stack
LeetCode 155:最小栈 Min Stack 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- ...
- 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 ...
- leetcode 155. Min Stack --------- java
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- Java [Leetcode 155]Min Stack
题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant ti ...
- LeetCode算法题-Min Stack(Java实现)
这是悦乐书的第177次更新,第179篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第36题(顺位题号是155).设计一个支持push,pop,top和在恒定时间内检索最小 ...
- 155. Min Stack
题目: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time ...
- leetCode Min Stack解决共享
原标题:https://oj.leetcode.com/problems/min-stack/ Design a stack that supports push, pop, top, and ret ...
随机推荐
- Eclipse UML插件Green UML、AmaterasUML
一.Green UML插件 1.查看Eclipse版本 查看当前电脑上安装的Eclipse版本(Help-About Eclipse Platform),是3.3.2版本的. 2.查看相应插件版本 然 ...
- centos update git(转载)
From:http://itekblog.com/update-git-centos/ 1.下载RPMForge repo cd /tmp # bit: wget http://pkgs.repofo ...
- Bug 是改不完滴
- using inno setup uninstall default icon
If you set SetupIconFile then the Uninstall Exe File (e.g. unins000.exe) will have exactly same icon ...
- Longest Increasing Subsequence(DP)
public static int LIS(List<Integer> al) { int[] arr = new int[al.size()]; int lis = 0; arr[0] ...
- strstr、strcmp、strlen、strcpy
const char* strstr(const char *str, const char* substr) { int i, j, temp; ; str[i] != '\0'; i++) { j ...
- '@P0' 附近有语法错误
问题出在ibatis中的某个orm配置文件,查看你的某些sql语句,尤其是用到#和$等进行赋值的,区分开到底是用#还是$ eg: select top $pagefrom$ id from tb_bo ...
- 一个简单的AJAX实例
创建一个简单的XMLHttpRequest,从一个TXT文件中返回数据. 来源于菜鸟教程 <!DOCTYPE html><html><head><meta c ...
- TortoiseSVN期望文件系统格式在“1”到“6”之间;发现格式“7”
安装好Subversion和TortoiseSVN之后.检出和浏览版本库的时候一直报错 "期望文件系统格式在"1"到"6"之间;发现格式"7 ...
- [转]Markdown 语法手册
Markdown 是一种轻量级标记语言,能将文本换成有效的XHTML(或者HTML)文档,它的目标是实现易读易写,成为一种适用于网络的书写语言. Markdown 语法简洁明了,易于掌握,所以用它来写 ...