问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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. C++语法小记---少见的语法之一

    很少用,列出来,便于理解和熟悉!!! // 1.单独使用位域限定符 ::xxx() //调用全局函数xxx // 2.全局重载new和delete T* tmp = (T*)(::operator n ...

  2. Flutter 实现酷炫的3D效果

    老孟导读:此文讲解3个酷炫的3D动画效果. 下面是要实现的效果: Flutter 中3D效果是通过 Transform 组件实现的,没有变换效果的实现: class TransformDemo ext ...

  3. Flutter-Tips

    1.报错:flutter: Another exception was thrown: Could not find a generator for route RouteSettings原因是一个工 ...

  4. Unity 基于excel2json批处理读取Excel表并反序列化

    excel2json是一款将Excel表格文件快速生成json和C#数据类的高效插件,详情了解如下: https://neil3d.github.io/coding/excel2json.html 该 ...

  5. 把若依管理系统部署到Linux

    一.前言 1.非常感谢若依作者为大家提供的非常优质的开源web项目,非常感谢!!! 2.若依官方文档:http://doc.ruoyi.vip/ruoyi/ 3.若依官方链接: 1)若依管理系统官方体 ...

  6. 【Vue组件通信】props、$ref、$emit,组件传值

    1.什么是组件通信 组件间如何通信,也就成为了vue中重点知识,组件通信,涉及到组件之间数据的传递.类似NET POST/GET参数传递. Vue基本的三种传递方式** (props.\(ref.\) ...

  7. 开源项目推荐 - 巨鲸任务调度平台(Spark、Flink)

    Big Whale(巨鲸),为美柚大数据研发的大数据任务调度平台,提供Spark.Flink等离线任务的调度(支持任务间的依赖调度)以及实时任务的监控,并具有批次积压告警.任务异常重启.重复应用监测. ...

  8. python beautifulsoup基本用法-文档搜索

    以如下html段落为例进行介绍 <html> <head> <title>The Dormouse's story</title> </head& ...

  9. C#中使用ajax请求

    ajax简介 Ajax 即“Asynchronous Javascript And XML”(异步 JavaScript 和 XML),是指一种创建交互式.快速动态网页应用的网页开发技术,无需重新加载 ...

  10. 路径总和(leetcode 113)

    题目描述如下所示: 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径.(https://leetcode-cn.com/problems/path-sum-ii/) ...