问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4020 访问。

设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈。

push(x) -- 将元素 x 推入栈中。

pop() -- 删除栈顶的元素。

top() -- 获取栈顶元素。

getMin() -- 检索栈中的最小元素。

MinStack minStack = new MinStack();

minStack.push(-2);

minStack.push(0);

minStack.push(-3);

minStack.getMin();   --> 返回 -3.

minStack.pop();

minStack.top();      --> 返回 0.

minStack.getMin();   --> 返回 -2.


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.

MinStack minStack = new MinStack();

minStack.push(-2);

minStack.push(0);

minStack.push(-3);

minStack.getMin();   --> Returns -3.

minStack.pop();

minStack.top();      --> Returns 0.

minStack.getMin();   --> Returns -2.


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4020 访问。

public class Program {

    public static void Main(string[] args) {
var minStack = new MinStack(); minStack.Push(-2);
minStack.Push(0);
minStack.Push(-3); Console.WriteLine(minStack.GetMin()); minStack.Pop(); Console.WriteLine(minStack.Top());
Console.WriteLine(minStack.GetMin()); Console.ReadKey();
} public class MinStack { private List<int> _list = null; private int _minValue = int.MaxValue; private void ComputerMinValue() {
//题目要求,设计一个可以在常数时间内检索到最小元素的栈
if(_list.Count() != 0) {
_minValue = _list.Min();
} else {
_minValue = int.MaxValue;
}
} public MinStack() {
_list = new List<int>();
} public void Push(int x) {
_list.Add(x);
ComputerMinValue();
} public void Pop() {
_list.RemoveAt(_list.Count - 1);
ComputerMinValue();
} public int Top() {
return _list[_list.Count - 1];
} public int GetMin() {
return _minValue;
} } }

以上给出1种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4020 访问。

-3
0
-2

分析:

显而易见,考虑到运行库的使用,Push 和 Pop 的时间复杂度应当为  ,其它方法的时间复杂度为:  。

C#LeetCode刷题之#155-最小栈(Min Stack)的更多相关文章

  1. LeetCode 刷题笔记 155. 最小栈(Min Stack)

    tag: 栈(stack) 题目描述 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- 删除栈顶的元素 ...

  2. LeetCode 155:最小栈 Min Stack

    LeetCode 155:最小栈 Min Stack 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- ...

  3. [Swift]LeetCode155. 最小栈 | Min Stack

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

  4. C#LeetCode刷题-栈

    栈篇 # 题名 刷题 通过率 难度 20 有效的括号 C#LeetCode刷题之#20-有效的括号(Valid Parentheses) 33.0% 简单 42 接雨水   35.6% 困难 71 简 ...

  5. C#LeetCode刷题-设计

    设计篇 # 题名 刷题 通过率 难度 146 LRU缓存机制   33.1% 困难 155 最小栈 C#LeetCode刷题之#155-最小栈(Min Stack) 44.9% 简单 173 二叉搜索 ...

  6. Java实现 LeetCode 155 最小栈

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

  7. leetcode刷题记录——栈和队列

    题目 232.用栈实现队列 class MyQueue { private Stack<Integer> in = new Stack<>(); private Stack&l ...

  8. 【LeetCode】155. 最小栈

    155. 最小栈 知识点:栈:单调 题目描述 设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- 删 ...

  9. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

随机推荐

  1. 【RPA Starter第二课】Introduction to the UiPath Enterprise Platform UiPath企业平台简介

    Introduction to the UiPath Enterprise Platform UiPath 企业平台简介 课程目标: 了解UiPath实现RPA的步骤 描述每个UiPath解决方案的关 ...

  2. springboot application.yml配置学习

    一.背景 为了更好的使用springboot,所以看一下application.yml配置这块.主要是看数据绑定这块. 主要参考:https://www.hangge.com/blog/cache/d ...

  3. react实战 : 用矩阵思想做一个自适应布局容器组件

    需求是这样的. 有一个需要显示若干方块型元素的小区域 数量比较少的时候显示一排 数量比较多的时候显示两排 用 grid 不好,因为当数量为奇数的时候需要两排里面的元素都乖乖的居中显示. 用 flex ...

  4. javascript : 对象取值练习

    let obj = { "qqq":0, "www":0, "eee":0, "rrr":1, "ttt&qu ...

  5. 搞定 CompletableFuture,并发异步编程和编写串行程序还有什么区别?你们要的多图长文

    你有一个思想,我有一个思想,我们交换后,一个人就有两个思想 If you can NOT explain it simply, you do NOT understand it well enough ...

  6. 《Python测试开发技术栈—巴哥职场进化记》—前言

    写在前面 今年从4月份开始写一本讲Python测试开发技术栈的书,主要有两个目的,第一是将自己掌握的一些内容分享给大家,第二是希望自己能系统的梳理和学习Python相关的技术栈.当时我本来打算以故事体 ...

  7. shell 输出json格式的内容

    对于shell脚本的输出,如果要输出json格式的内容,我们可以借助python -m json.tool命令 比如 echo '{"name":"zhangsan&qu ...

  8. Caused by: java.sql.SQLSyntaxErrorException: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'c.id'

    打开mysql客户端,输入 select @@global.sql_mode 再执行 set @@global.sql_mode ='STRICT_TRANS_TABLES,NO_ZERO_IN_DA ...

  9. Python修改元组

    Python修改元组:元组中的元素值是不允许修改的,当创建好的时候就是固定不变的.所谓的修改其实是指创建一个新的元组,只是该元组可能是比原来的元组多一个元素或者少一个元素,然后使用新创建好的元组代替原 ...

  10. python基础day3_str基础函数操作方法及for循环

    字符串操作 s = 'uiehSdc hdsj$jfdks@' s1 = s.capitalize() #仅仅只首字母大写 print(s1) # 结果Uiehsdc s2 = s.upper() # ...