LintCode Min Stack
用两个stack, 第一个按顺序放所有值,第二个只放当前最小值。
注意: 1. 最小值有多个则都放到两个stack里, 尤其别忘放第二个; 2. pop时若两个stack的最上面值相等则都pop, 不等则只pop第一个stack, 但是都得返回第一个stack的pop值; 3. min时只返回第二个stack的peek值。
- public class MinStack {
- Stack<Integer> stack1 = new Stack<Integer>();
- Stack<Integer> stack2 = new Stack<Integer>();
- public MinStack() {
- // do initialize if necessary
- }
- public void push(int number) {
- stack1.push(number);
- if(stack2.empty()){
- stack2.push(number);
- } else{
- if(stack2.peek() >= number){
- stack2.push(number);
- }
- }
- // write your code here
- }
- public int pop() {
- if(stack1.empty() || stack2.empty()){
- return -1;
- }
- if(stack1.peek().equals(stack2.peek())){
- stack2.pop();
- return stack1.pop();
- } else{
- return stack1.pop();
- }
- // write your code here
- }
- public int min() {
- return stack2.peek();// write your code here
- }
- }
LintCode Min Stack的更多相关文章
- [LintCode] Min Stack 最小栈
Implement a stack with min() function, which will return the smallest number in the stack. It should ...
- [CareerCup] 3.2 Min Stack 最小栈
3.2 How would you design a stack which, in addition to push and pop, also has a function min which r ...
- leetcode 155. Min Stack --------- java
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- Min Stack [LeetCode 155]
1- 问题描述 Design a stack that supports push, pop, top, and retrieving the minimum element in constant ...
- Min Stack
Min Stack Design a stack that supports push, pop, top, and retrieving the minimum element in constan ...
- Java [Leetcode 155]Min Stack
题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant ti ...
- 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 ...
- LeetCode算法题-Min Stack(Java实现)
这是悦乐书的第177次更新,第179篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第36题(顺位题号是155).设计一个支持push,pop,top和在恒定时间内检索最小 ...
随机推荐
- cc1101 ASK发射模式
cc1101 配置 433.919830Mhz 1.19948kBaud 199.951172 58.035714 #ifndef _CC1100_H_#define _CC1100_H_ ...
- 导航效果css
<!doctype html> <html> <head> <meta charset="utf-8" /> <style&g ...
- React Native + Nodejs 使用RSA加密登录
想用rn做个RSA(非对称加密)登录 基本流程就是在服务端生成RSA后,将“公钥”发到客户端,然后客户端用“公钥”加密信息发送到服务端,服务务端用私钥解密. 过程不复杂,问题在于,nodejs和rn都 ...
- centos 6.6编译安装git-2.7.0 最新git编译安装
系统环境:centos6.6 1,安装准备 yum -y install gcc zlib-devel openssl-devel perl cpio expat-devel gettext-deve ...
- 初学js
接触时感觉跟前面写网页的差距和大,与c语言很相似.主要学的有: 1.引入js的三种方法:外联,内联,嵌套 2.标识符:第一个字符可以是任意Unicode字母,以及美元符号($)和下划线(_). - 第 ...
- java常用注释
@see 加入超链接 @see 类名 @see 完整类名 @see 完整类名#方法名 @version 版本信息 @author 作者信息 @param 参数名 说明 @return 说明 @exce ...
- 关于TortoiseGit使用的心得
花了我一个晚上,终于弄明白为什么总是 push 失败的原因了!竟然是因为我用的是注册的用户名而不是邮箱名……囧死. 另外搞清楚了一个问题,就是 Git 和远程仓库交互有两种方式,即 https 方式和 ...
- 使用Object类型的多态引用是会付出代价的
import java.util.*; public class FiveShi { String name; public void eat(){ System.out.println(" ...
- C# 水印透明度图片
/// <summary> /// 在一张图片的指定位置处加入一张具有水印效果的图片 /// </summary> /// <param name="Sourc ...
- web浏览器兼容简要整理
ajax的创建 if (window.XMLHttpRequest) { var xhr = new XMLHttpRequest(); } else { //IE6及其以下版本浏览器 var xhr ...