LeetCode之Min Stack
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.问题翻译
设计一个栈,使它能够同时支持 push, pop, top方法并且能够同时检索最小值
push(x) -- 把元素X放到栈中
pop() --将栈顶元素移除
top() --获取栈顶元素
getMin() --检索出栈中最小的值
3.解决思路
a:最简单直接的就是通过Java自己提供的栈,然后因为栈提供了出栈(pop())进栈(push())还有栈顶元素(peek())的方法,我们需要做的就是宁外设置一个变量或者使用一个其他的栈来保持最小的元素,在进栈出栈的时候来更新这个值,从而就可以获取到了最小值了
b:不使用Java提供的内置栈的实现方式,这个需要我们来模仿栈的实现过程
4.实现方式(b)
节点的定义(Node.java)
package minStack; /**
* 栈中的节点定义(保存节点元素的同时需要保存栈中最小的元素的值)
* @author wudy
*
*/
class Node { int value;//当前元素的值
int minValue;//用来保存栈中最小的元素(只需要在站定元素中设置这个属性就行了)
Node next;//指向下一节点,模仿栈的实现 /**
* 构造函数,实例化栈的节点
* @param value
*/
public Node(int value) {
this.value = value;
}
}
最小值栈的实现(MinStack.java)
package minStack; class MinStack { Node top = null; /**
* 入栈,在入栈的同时需要检查更新元素的最小值
* @param x
*/
public void push(int x) {
if (top == null) {
top = new Node(x);
top.minValue = x;
} else {
Node temp = new Node(x);
temp.next = top;
top = temp;
top.minValue = Math.min(top.next.minValue, x);
}
} /**
* 出栈,只需要将当前元素节点的指针指向下一个元素即可
*/
public void pop() {
top = top.next;
return;
} /**
* 获取栈顶元素(不为空则返回具体的值)
* @return
*/
public int top() {
return top == null ? 0 : top.value;
} /**
* 从栈顶元素中拿到最小值
* @return
*/
public int getMin() {
return top == null ? 0 : top.minValue;
}
}
测试类(MinStackTest.java)
package minStack; public class MinStackTest { /**
* @param args
*/
public static void main(String[] args) { MinStack minStack = new MinStack(); for(int index = 0;index < 10;index++){
minStack.push(index);
} System.out.println("栈顶元素:"+minStack.top());
System.out.println("最小元素:"+minStack.getMin());
System.out.println("栈顶元素出栈"); minStack.pop(); System.out.println("栈顶元素:"+minStack.top());
System.out.println("最小元素:"+minStack.getMin());
} }
5.主要考察了数据结构中栈的实现和定义,比较简单
LeetCode之Min Stack的更多相关文章
- 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 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(最小栈)
翻译 设计支持push.pop.top和在常量时间内检索最小元素的栈. push(x) -- 推送元素X进栈 pop() -- 移除栈顶元素 top() -- 得到栈顶元素 getMin() -- 检 ...
- [LeetCode] 0155. Min Stack 最小栈 & C++Runtime加速
题目 Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. ...
- 【LeetCode】Min Stack 解题报告
[题目] Design a stack that supports push, pop, top, and retrieving the minimum element in constant tim ...
- 【leetcode】Min Stack -- python版
题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant ti ...
- Java for LeetCode 155 Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- LeetCode之Min Stack 实现最小栈
LeetCode相关的网上资源比较多,看到题目一定要自己做一遍,然后去学习参考其他的解法. 链接: https://oj.leetcode.com/problems/min-stack/ 题目描述: ...
随机推荐
- 浅析pinyin4j源码 简单利用pinyin4j对中文字符进行自然排序(转)
pinyin4j项目 官网地址 http://pinyin4j.sourceforge.net/ 我们先把资源下载下来,连同源码和jar包一起放入工程.如下图: 接下来在demo包下,我们写一个测试 ...
- cocos2d-x3.0 windows 环境配置
cocos2d-x3.0 windows 环境配置 参考Oo泡泡糖oO的CSDN博文 :http://blog.csdn.net/u010296979/article/details/24273393 ...
- 具体解释首页被K后SEOer必做的三大排除方法!
近段时间.有非常多朋友向新辰抱怨说出大问题了,为神马site不到首页了,并且收录变成了0?唉,新辰不得不非常同情的告诉你:你的首页真的被K了!好了.作为一个职业SEOer.面对被K宛如已经看破红尘般没 ...
- poj3903 Stock Exchange(最长上升子序列)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:id=3903">http://poj.org/problem?id=3903 Descrip ...
- j2se--Socket沟通
第一次接触Socket课程设计大二,我在做一个图书馆管理系统.源代码是从互联网上下载,代码天天磨,隐约中记得有Socket这么一个单词. 第二次是去年代表学校參加"河北省电子信息职业技能大赛 ...
- HDU 1505 Largest Rectangle in a Histogram && HDU 1506 City Game(动态规划)
1506意甲冠军:给你一个连续的直方图(拼贴底部长度1).求连续基质区. 对每一个直方图,分别向左向右进行扩展. #include<cstdio> #include<stdlib.h ...
- hadoop 开始时间datanode一个错误 Problem connecting to server
刚刚配置hadoop,namenode常开,但datanode但保留了错误.但不启动: 2014-05-04 10:43:33,970 WARNorg.apache.hadoop.hdfs.serve ...
- JAVA连接ACCESS、MYSQL、SQLSEVER、ORACLE数据库
. 概要 1.1 JDBC概念 JDBC(Java Database Connectivity)是Java语言为了支持SQL功能而提供的与数据库连接的用户的接口.JDBC中包含了一组由(Java)语言 ...
- Android开发模板------自己定义SimpleCursorAdapter的使用
使用SimpleCursorAdapter所设计的table(数据表)一定要有_id字段名称,否则会出现"找不到_id"的错误 SimpleCursorAdapter直接使用的方法 ...
- android在单身的对象和一些数据的问题被释放
正式接触android我们一直在开发了一段时间,该项目的第一个版本最终会很快结束. 当有它自己的测试.拥有android后台.同一时候打开了几个应用之后又一次切回到自己的app.发现报错了.经过排查, ...