【leetcode】Min Stack(easy)
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.
思路:用栈
class MinStack {
public:
void push(int x) {
if(mins.empty() || x <= mins.top())
mins.push(x);
s.push(x);
}
void pop() {
if(!s.empty() && !mins.empty() && s.top() == mins.top())
mins.pop();
s.pop();
}
int top() {
return s.top();
}
int getMin() {
return mins.top();
}
private:
stack<int> s;
stack<int> mins;
};
【leetcode】Min Stack(easy)的更多相关文章
- 【leetcode】Happy Number(easy)
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- 【leetcode】Same Tree(easy)
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- 【leetcode】Remove Element (easy)
Given an array and a value, remove all instances of that value in place and return the new length. T ...
- 【leetcode】Count Primes(easy)
Count the number of prime numbers less than a non-negative number, n 思路:数质数的个数 开始写了个蛮力的,存储已有质数,判断新数字 ...
- 【LeetCode】栈 stack(共40题)
[20]Valid Parentheses (2018年11月28日,复习, ko) 给了一个字符串判断是不是合法的括号配对. 题解:直接stack class Solution { public: ...
- 【leetcode】Min Stack -- python版
题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant ti ...
- 【LeetCode】Min Stack 解题报告
[题目] Design a stack that supports push, pop, top, and retrieving the minimum element in constant tim ...
- 【leetcode】Reverse Integer(middle)☆
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...
- 【LeetCode】Reconstruct Itinerary(332)
1. Description Given a list of airline tickets represented by pairs of departure and arrival airport ...
随机推荐
- Todd's Matlab讲义第6讲:割线法
割线法 割线法求解方程\(f(x)=0\)的根需要两个接近真实根\(x^\*\)的初值\(x_0\)和\(x_1\),于是得到函数\(f(x)\)上两个点\((x_0,y_0=f(x_0))\)和\( ...
- HDOJ 3593 The most powerful force
树形DP / 泛化物品的背包...可以去看09年徐持衡论文<浅谈几类背包问题> The most powerful force Time Limit: 16000/8000 MS (Jav ...
- mongoDB- - 2 增、删、改 操作
1.创建数据库 语法:use database 说明:如果database不存在,就会创建database:如果存在就会切换到database 2.查看所有数据库 语法:show dbs; 说明:如果 ...
- 1-File类的使用
package com.io; import java.io.File; import java.io.FileInputStream; import java.io.IOException; imp ...
- Javascript高级程序设计——this、闭包、函数表达式
在javascript中函数声明会被提升,而函数表达式不会被提升.当函数执行时,会创建一个执行环境和相应的作用域链,然后利用arguments和其他的命名参数的值来初始化函数的活动对象,作用域链链中所 ...
- Flash+XML前后按钮超酷焦点图,层叠翻转图形
Flash+XML,有“前后”按钮,可以左右点击,支持鼠标滚轮,效果流畅,推荐下载.大图尺寸:680x345 点击下载
- iOS开发——UI基础-按钮内边距,图片拉伸
一.内边距 UIButton有三个属性,分别可以设置按钮以及内部子控件的内边距 1.contentEdgeInsets 如果是设置contentEdgeInsets, 会把UIImageView和UI ...
- [转载]Eclipse.ini的相关说明
原文链接:http://www.cnblogs.com/yan5lang/archive/2011/05/24/2055867.html Eclipse的启动由$ECLIPSE_HOME/eclips ...
- metasploit连接数据库
表示一直出问题.不造哪里出错.望有知情人与本人取得联系! test
- 分享一个快速设置背景的js 自动获取背景图的长宽
我来分享一个快速设置背景的js (需要jq支持!) 快速切图铺页面用---就是不需要手动输入背景图的长宽 自动获取背景图的长宽 : <div class="wrap"> ...