LintCode:带最小值操作的栈
来源:互联网 发布:万网如何设置三级域名 编辑:程序博客网 时间:2024/06/05 10:59
LintCode:带最小值操作的栈
Python
class MinStack(object): def __init__(self): # do some intialize if necessary self.stack = [] def push(self, number): # write yout code here if len(self.stack) == 0: self.stack.append([number, number]) else: self.stack.append([number, min(number, self.stack[-1][1])]) def pop(self): # pop and return the top item in stack return self.stack.pop()[0] def min(self): # return the minimum number in stack return self.stack[-1][1]
Java
public class MinStack { private Stack<ArrayList<Integer>> stack; public MinStack() { // do initialize if necessary stack = new Stack<ArrayList<Integer>>(); } public void push(int number) { // write your code here if(stack.empty()){ ArrayList<Integer> L = new ArrayList(Arrays.asList(number, number)); stack.push(L); } else{ ArrayList<Integer> tmp = stack.peek(); if(tmp.get(1) < number){ ArrayList<Integer> L = new ArrayList(Arrays.asList(number, tmp.get(1))); stack.push(L); } else{ ArrayList<Integer> L = new ArrayList(Arrays.asList(number, number)); stack.push(L); } } } public int pop() { // write your code here ArrayList<Integer> L = stack.pop(); int ans = L.get(0); return ans; } public int min() { // write your code here ArrayList<Integer> L = stack.peek(); int ans = L.get(1); return ans; }}
0 0
- LintCode 带最小值操作的栈
- lintcode-带最小值操作的栈-12
- LintCode:带最小值操作的栈
- lintcode,带最小值操作的栈
- lintcode,带最小值操作的栈
- LintCode 12 带最小值操作的栈
- lintcode -- 带最小值操作的栈
- [LintCode]12.带最小值操作的栈
- LintCode-----12.带最小值操作的栈
- lintcode带最小值操作的栈
- Lintcode 12. 带最小值操作的栈
- Lintcode 带最小值的栈
- LintCode-剑指Offer-(12)带最小值操作的栈
- [LintCode] 带最小值操作的栈 Min Stack
- [未解决][lintcode]带最小值操作的栈
- Google/LintCode:M-带最小值操作的栈
- (lintcode)第12题带最小值操作的栈
- [LintCode 12] 带最小值操作的栈(Python)
- JDBC入门教程(三) PreparedStatement的使用
- HOWTO make ns-3 interact with the real world
- 个人小摘录
- 解决之前App内语言切换的某些bug
- E - Gerald's Hexagon
- LintCode:带最小值操作的栈
- boost::string
- SQLite 点命令简介
- Category在开发中使用的淫技之——改变系统方法
- 深入理解String为什么是不可变的类
- Unity资源路径及加载外部资源介绍
- gunzip -9代表什么
- BZOJ 3168: [Heoi2013]钙铁锌硒维生素
- JS操作JSON总结