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/ 题目描述: ...
随机推荐
- 当一个线程进入一个对象的一个synchronized方法后,其它线程是否可进入此对象的其它方法(转)
对象的synchronized方法不能进入了,但它的其他非synchronized方法还是可以访问的 对每一个class只有一个thread可以执行synchronized static method ...
- oracle 11g 基于磁盘的备份rman duplicate
基于磁盘的备份rman duplicate 命令创建standby database 前提条件: 确保原始库数据库的备份.存档standby 结束是完全可见, 这里,如果原始文库和靶 - 侧数据文件, ...
- 【翻译】在Ext JS 5应用程序中怎样使用路由
原文:How to Use Routing in Your Ext JS 5 Apps 简单介绍 Ext JS 5是一个重要的公布版本号,它提供了很多新特性来创建丰富的.企业级的Web应用程序.MVV ...
- MVC 快速开发框架
ASP.NET MVC 快速开发框架之 SqlSugar+SyntacticSugar+JQWidgetsSugar+jqwidgets jqwidgets.js: 是一个功能完整的框架,它具有专业的 ...
- UVALive 4730 Kingdom +段树和支票托收
主题链接:点击打开链接 题意见白书P248 思路: 先把读入的y值都扩大2倍变成整数 然后离散化一下 用线段树来维护y轴 区间上每一个点的 城市数量和联通块数量. 然后用并查集维护每一个联通块及联通块 ...
- HDU ACM 1007 Quoit Design 分而治之的方法,最近点
意甲冠军:给n坐标点.半一对点之间的距离所需的距离最近. 分析:分而治之的方法,最近点. #include<iostream> #include<algorithm> #inc ...
- Visual Studio 2010/2013 查看DLL接口(函数)
1. “应用程序" Visual Studio 2010/2013 的Visual Studio Tools文件夹中打开Visual Studio Command Prompt 命令提示窗口 ...
- 第20章 状态模式(State Pattern)
原文 第20章 状态模式(State Pattern) 状态模式 概述: 当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类. 状态模式主要解决的是当控制一个对象状态的条件表 ...
- MVC4 + EF为Model添加单独的验证属性
可使用以下方式给Model加上相关的meta验证属性,这样实体的验证属性就不会被例如EF或其他工具自动生成的Model所替换了. using System.ComponentModel.DataAnn ...
- 使用php-fpm状态页观察当前的php-fpm状态
对于php-fpm的參数设置,非常多情况下有这种疑问,就是内置的几个參数比如pm.max_children,pm.start_servers等这几个參数究竟该设置最多为多少才合适.事实上这几个參数往往 ...