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的更多相关文章

  1. leetcode 155. Min Stack --------- java

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

  2. Java [Leetcode 155]Min Stack

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

  3. 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 ...

  4. LeetCode 155 Min Stack(最小栈)

    翻译 设计支持push.pop.top和在常量时间内检索最小元素的栈. push(x) -- 推送元素X进栈 pop() -- 移除栈顶元素 top() -- 得到栈顶元素 getMin() -- 检 ...

  5. [LeetCode] 0155. Min Stack 最小栈 & C++Runtime加速

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

  6. 【LeetCode】Min Stack 解题报告

    [题目] Design a stack that supports push, pop, top, and retrieving the minimum element in constant tim ...

  7. 【leetcode】Min Stack -- python版

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

  8. Java for LeetCode 155 Min Stack

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

  9. LeetCode之Min Stack 实现最小栈

    LeetCode相关的网上资源比较多,看到题目一定要自己做一遍,然后去学习参考其他的解法. 链接: https://oj.leetcode.com/problems/min-stack/ 题目描述: ...

随机推荐

  1. cocos2d-html5基金会

    1 环境结构 版本号Cocos2d-html5-v2.2,tomcat7.0 构造tomcat.然后直接解压Cocos2d-html5-v2.2.zip.解压后根文件访问的文件夹index.html你 ...

  2. UVALive - 3263 That Nice Euler Circuit (几何)

    UVALive - 3263 That Nice Euler Circuit (几何) ACM 题目地址:  UVALive - 3263 That Nice Euler Circuit 题意:  给 ...

  3. JAVA必备——Struts

    在我们的开发中,有一个经典的框架,就是SSH,他们各自是:Struts,Spring,Hibernate,我们队他们神交已久,在曾经的博客中我介绍了Hibernate,今天我们也逐步揭开Struts的 ...

  4. UOJ #5. 【NOI2014】动物园 扩大KMP

    第一次NOI称号. ... 扩展假设知道KMP如果. .. . 就是水题了. ... #5. [NOI2014]动物园 统计提交情况 描写叙述 提交 近日.园长发现动物园中好吃懒做的动物越来越多了.比 ...

  5. 【软件project】生存期模型(含图)

    为了反映软件生存周期内各个工作应怎样组织,各阶段怎样衔接,须要软件开发模型给出直观图示表达.软件开发模型是软件思想的详细化,是实施在过程模块中的软件开发方法和工具. 以下来介绍开发模型的特点以及他们的 ...

  6. java 选择文件夹对话框

    java swing 选择文件夹对话框 import java.io.File; import javax.swing.JFileChooser; public class Test2 { publi ...

  7. 《得知opencv》注意事项——矩阵和图像处理——cvOr,cvOrS,cvrReduce,cvRepeat,cvScale,cvSet and cvSetZero

    矩阵和图像的操作 (1)cvOr函数 其结构 void cvOr(//两个矩阵相应元素做或执行 const CvArr* src1,//矩阵1 const CvArr* src2,//矩阵2 CvAr ...

  8. do...while(0)神奇

    1. do...while(0)消除goto语句. 通常,假设在一个函数中開始要分配一些资源.然后在中途运行过程中假设遇到错误则退出函数,当然,退出前先释放资源,我们的代码可能是这样: version ...

  9. Java设计模式菜鸟系列(四)工厂方法模式建模与实现

    转载请注明出处:http://blog.csdn.net/lhy_ycu/article/details/39760895 工厂方法模式(Factory Method) 工厂方法:顾名思义,就是调用工 ...

  10. hadoop得知;datajoin;chain署名;combine()

    hadoop一种简化机制来管理job和control作业之间的非线性依赖,job对象mapreduce表明. job该目的是通过使实例化jobconf对象的构造函数的工作落实. x.addDeopen ...