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/ 题目描述: ...
随机推荐
- VS路宏 vc++于OutDir、ProjectDir、SolutionDir不同的路径
说明 $(RemoteMachine) 设置为"调试"属性页上"远程计算机"属性的值.有关很多其它信息,请參见更改用于 C/C++ 调试配置的项目设置. $(R ...
- SQL Mirroring[Hot back up with Double machine]
Background: It's fairly common for businesses to want to provide some high availability for their SQ ...
- 利用System.Net.Mail 的SmtpClient发送邮件
原文:利用System.Net.Mail 的SmtpClient发送邮件 几个月前总结过关于Jmail发送邮件,当时用Jmail发送邮件发送速度有点慢(可能对Jmail了解不是很多).现在改为用微软提 ...
- hdu 4445 Crazy Tank (暴力枚举)
Crazy Tank Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 排序(4)---------希尔(shell)排序(C语言实现)
由于考试耽搁了几天,不好意思~~~ 前面的介绍的三种排序算法,都属于简单排序,大家能够看下详细算法,时间复杂度基本都在0(n^2),这样呢,非常多计算机界.数学界的牛人就非常不爽了,他们在家里想啊想, ...
- python fabric远程操作和部署
博客迁往:新地址(点击直达) 新博客使用markdown维护,线下有版本号库,自己写的所以会定时更新同步.同一时候提供更好的导航和阅读体验 csdn对markdown支持不好.所以旧版不会花时间进行同 ...
- Custom Data Service Providers
Custom Data Service Providers Introduction Data Services sits above a Data Service Provider, which i ...
- 软体project(四)——一生
软件生存周期是软件project中的一个重要概念,把整个生存周期划分为若干个阶段,是实现软件生产project化的重要步骤. 软件的生存周期一般划分为软件计划.软件开发和软件执行三个时期,例如以下图: ...
- linux下一个eclipse组态jdk
今天ubuntu12.04安装eclipse,安装该想法eclipse后.还需要配置jdk.但没想到eclipse我有自己主动做好(但最主要的原因是我的linux在刚刚安装了一个jdk,假设有两个或更 ...
- python_在windows下安装配置python开发环境及Ulipad开发工具
最近开始学习Python,在网上寻找一下比较好的IDE.因为以前用C#做开发的,用Visual Studio作为IDE,鉴于用惯了VS这么强大的IDE,所以对IDE有一定的依赖性. Python的ID ...