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/ 题目描述: ...
随机推荐
- yii组态 redis主从配置(随着代码)
最近的欲望redis 主从,但yii内建的redis 它不支持主从.不仅写了一个好办法 结构例,以下: 1.main.php通过添加下面的句子: //redis缓存配置 'cache_redis' = ...
- 编写高质量JavaScript代码绳之以法(The Essentials of Writing High Quality JavaScript)翻译
原文:The Essentials of Writing High Quality JavaScript 才华横溢的Stoyan Stefanov,在他写的由O'Reilly初版的新书<Java ...
- Unity3D根据游戏的发展Terrain Toolkit地形生产
今天我们继续给我Unity3D游戏开发系列.今天我们来通过Terrain Toolkit为了使地形. 虽然Unity3D它为我们提供了一个地形渲染工具,我们发现,这个地形绘制工具并不能满足我们的 ...
- ActiveMQ与RabbitMQ采用camel综合
著名EIP实施框架Camel它起源于ActiveMQ的一些基于消息的集成需求.然后逐渐发展成为一个ActiveMQ的子项目,最后这一块的功能越来越完好.就成为了Apache的顶级项目. 所以,从一開始 ...
- java自动转型
/*2015-10-30*/ public class TypeAutoConvert { public static void main(String[] args) { int a = 5; Sy ...
- Matrix (二维树状数组)
Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...
- 组态ORACLE 11G ADG
一旦载10g的,没有票据.昨天使用duplicate方法一安装11g ADG,过程艰辛,记录: 一.环境配置 主图书馆 IP地址:192.168.233.128/24 操作系统版本号:rhel5.8 ...
- oracle_常用命令(表空间查询)
---查询表空间 对应的物理文件,名称 ,大小 ,已用的,利用率 SELECT B.FILE_NAME "FILE_NAME", B.TABLESPACE_NAME "T ...
- MIFARE系列6《射频卡与读写器的通信》
1. ATR(Answer to request) 读写器呼叫磁场内的卡片.卡片对呼叫做出应答. 对刚进入磁场得到电复位处于休闲状态的卡片,卡请求(REQA,0x26):对于已进行过读写操作并进入休眠 ...
- 【转】Android 常用 adb 命令总结
原文地址:http://testerhome.com/topics/2565 针对移动端 Android 的测试, adb 命令是很重要的一个点,必须将常用的 adb 命令熟记于心, 将会为 Andr ...