栈的实现,多加了一个最小值的获取

class MinStack {
public:
struct Node {
int nNum;
int nMinNum;
Node* pNext;
Node() {
pNext = nullptr;
}
};
/** initialize your data structure here. */
MinStack() {
m_pHead = nullptr;
} ~MinStack() {
while(m_pHead != nullptr) {
Node* pTmp = m_pHead;
m_pHead = m_pHead->pNext;
delete pTmp;
pTmp = nullptr;
}
} void push(int x) {
if(m_pHead == nullptr) {
m_pHead = new Node;
m_pHead->pNext = nullptr;
m_pHead->nMinNum = x;
}
else {
Node* pTmp = new Node;
pTmp->nMinNum = m_pHead->nMinNum;
pTmp->pNext = m_pHead;
m_pHead = pTmp;
if(m_pHead->nMinNum > x) {
m_pHead->nMinNum = x;
}
}
m_pHead->nNum = x;
} void pop() {
if(m_pHead != nullptr) {
Node* pTmp = m_pHead;
m_pHead = m_pHead->pNext;
delete pTmp;
pTmp = nullptr;
}
} int top() {
if(m_pHead != nullptr) {
return m_pHead->nNum;
}
return ;
} int getMin() {
return m_pHead->nMinNum;
} protected:
Node* m_pHead;
};

可关注公众号了解更多的面试技巧

LeetCode_155-Min Stack的更多相关文章

  1. [LintCode] Min Stack 最小栈

    Implement a stack with min() function, which will return the smallest number in the stack. It should ...

  2. [CareerCup] 3.2 Min Stack 最小栈

    3.2 How would you design a stack which, in addition to push and pop, also has a function min which r ...

  3. leetcode 155. Min Stack --------- java

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

  4. Min Stack [LeetCode 155]

    1- 问题描述 Design a stack that supports push, pop, top, and retrieving the minimum element in constant ...

  5. Min Stack

    Min Stack Design a stack that supports push, pop, top, and retrieving the minimum element in constan ...

  6. Java [Leetcode 155]Min Stack

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

  7. 155. Min Stack

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

  8. leetCode Min Stack解决共享

    原标题:https://oj.leetcode.com/problems/min-stack/ Design a stack that supports push, pop, top, and ret ...

  9. LeetCode算法题-Min Stack(Java实现)

    这是悦乐书的第177次更新,第179篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第36题(顺位题号是155).设计一个支持push,pop,top和在恒定时间内检索最小 ...

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

随机推荐

  1. Erlang中的RSA签名

    RSA签名校验 -spec check_rsa_sign(DataBin, Sign, RSAPublicKeyBin, DigestType) -> boolean when DataBin ...

  2. 2019-2020-1 20199322《Linux内核原理与分析》第一周作业

    图解sudo deluser name和sudo deluser name --remove -home的区别? 先众所周知地创建一个用户“hanmeimei” 然后给韩梅梅创建一个二级的目录,并且在 ...

  3. .Net基础篇_学习笔记_第五天_流程控制do-while循环

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  4. eclipse搭建springmvc

    https://www.cnblogs.com/qixing/p/qixing.html

  5. 从原理到场景 系统讲解 PHP 缓存技术

    第1章课程介绍 此为PHP相关缓存技术的课堂,有哪些主流的缓存技术可以被使用? 第1章 课程介绍 1-1课程介绍1-2布置缓存的目的1-3合理使用缓存1-4哪些环节适合用缓存 第2章 文件类缓存 2- ...

  6. 04 (OC)* weak的实现原理

    一:Weak 表 1: Runtime 维护了一个 Weak 表,用于存储所有 Weak 指针.Weak 表是一个哈希表,Key 是对象的地址,Value 是一个数组,数组里面放的是 Weak 指针的 ...

  7. C++基础之泛型算法

    标准库并未给每个容器添加大量功能,因此,通过大量泛型算法,来弥补.这些算法大多数独立于任何特定的容器,且是通用的,可用于不同类型的容器和不同的元素. 迭代器使得算法不依赖容器,但是算法依赖于元素的类型 ...

  8. 【Jsp】利用iframe实现action不跳转

    <form role="form" target="id_frame" action="dk" method="post&q ...

  9. Flask中的路由、实例化参数和config配置文件

    Flask中的路由 endpoint 别名不能重复,对应的视图函数,默认是视图函数名.endpoint 才是路由的核心.视图函数与路由的对应关系.可以通过url_for 反向创建url # metho ...

  10. 对Servlet执行流程的初步认识

    这里我们以Post方式请求Serclet为例 1.找到 中的URL地址 Form表单的Post请求HelloServlet(Action="HelloServlet")发起时, A ...