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.

解题思路:

本题是 《JAVA语言程序设计-基础篇》的原题,修改下即可,JAVA实现如下:

public class MinStack {
private int[] elements;
private int size; public MinStack() {
elements = new int[16];
} public void push(int x) {
if (size >= elements.length) {
int[] temp = new int[elements.length * 2];
System.arraycopy(elements, 0, temp, 0, elements.length);
elements = temp;
}
elements[size++] = x;
} public void pop() {
size--;
} public int top() {
return elements[size - 1];
} public int getMin() {
int min=Integer.MAX_VALUE;
for(int i=0;i<size;i++)
if(min>elements[i])
min=elements[i];
return min;
}
}

Java for LeetCode 155 Min Stack的更多相关文章

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

  2. leetcode 155. Min Stack --------- java

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

  3. Java [Leetcode 155]Min Stack

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

  4. LeetCode 155 Min Stack(最小栈)

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

  5. [LeetCode] 155. Min Stack 最小栈

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

  6. Leetcode 155 Min Stack 小顶堆+栈,优先队列实现 难度:0

    https://leetcode.com/problems/min-stack/ #include <vector> #include <queue> #include < ...

  7. Leetcode 155 Min Stack

    题意:设计一个能输出栈内最小值的栈 该题设计两个栈,一个栈是正常的栈s,而另一个是存最小值的栈sm 在push时要判断sm是否为空,如果为空或者非空但是栈顶元素大于等于插入值的 需要在sm中插入x 同 ...

  8. 155. Min Stack

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

  9. Java实现 LeetCode 155 最小栈

    155. 最小栈 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) – 将元素 x 推入栈中. pop() – 删除栈顶的元素. top() – 获取 ...

随机推荐

  1. Mathematical operation

    (1)Using let let result=2+1 let result=2-1 let result=2*1 let result=2/1(2) Using bracket echo $(($p ...

  2. JEECMS页面中常用标签

    (1)在段落前给每一行加序号 从0开始:${a_index}从1开始:${a_index+1} (2)标记说明 [文章导航]:[@cms.Position /] [文章标题]:${arti.title ...

  3. Linux虚拟地址空间布局

    在多任务操作系统中,每个进程都运行在属于自己的内存沙盘中.这个沙盘就是虚拟地址空间(Virtual Address Space),在32位模式下它是一个4GB的内存地址块.在Linux系统中, 内核进 ...

  4. com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database 'db_item'

    一直很奇怪,为什么报错,进入mysql命令行,show databases:发现多谢了一个空格,如上图.

  5. Dedecms include\dialog\select_soft_post.php Upload Any Files To The Specified Directory Via Variable Not Initial Flaw Bypass Extension Defence

    目录 . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 综合来说,这个漏洞的根源是"register_globals = on& ...

  6. uva 10723 Cyborg Genes(LCS变形)

    题目:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=107450#problem/C 题意:输入两个字符串,找一个最短的串,使得输入的两个 ...

  7. linux 破解版confluence安装

    OS  centos 6.5  需要的安装包如下: jre-7u67-linux-x64.rpm atlassian-confluence-5.4.4-x64.bin mysql-connector- ...

  8. ExtJS入门教程02,form也可以很优雅

    在上一篇<Extjs window 入门>中,我们已经看到了如何将一个form组件放到window中,今天我们来看看form的一些优雅的工作方式. 使用fieldDefaults,优雅的设 ...

  9. 代码重构-1 对参数中有 bool值的拆分

    最近对一个同事的代码进行重构 第1步 对参数中有 bool值的拆分 原代码如下: private bool CheckIsInFreeTimes(GetDataForValidateLotteryRe ...

  10. linux学习之用户管理

    用户管理是在root用户下进行相关操作的 1.配置文件路径:         保存用户信息的文件:/etc/passwd         保存密码的文件:/etc/shadow         保存用 ...