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

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. 浅谈contentType = false

    转自https://segmentfault.com/a/1190000007207128 在刚接触 JQuery 中的 ajax 时,对其 contentType 并没有很在意,只是知晓它是代表发送 ...

  2. Linux OOM-killer(内存不足时kill高内存进程的策略)

    OOM_killer是Linux自我保护的方式,当内存不足时不至于出现太严重问题,有点壮士断腕的意味 在kernel 2.6,内存不足将唤醒oom_killer,挑出/proc/<pid> ...

  3. 洛谷 P1219八皇后

    把全部,在这251秒,赌上! ——<游戏人生zero> 题目:https://www.luogu.org/problem/P1219 八皇后是一道非常非常非常经典的深搜+回溯的题目. 这道 ...

  4. Unity3D_03_代码及效率优化总结

    1.在使用数组或ArrayList对象时应当注意: length = myArray.Length; ;i<length;i++) { } 避免 ;i<myArray.Length;i++ ...

  5. java.io.IOException: 设备上没有空间

    解决: 逐层目录查找最大文件夹du -h --max-depth=1 确定最大目录为log目录,删除log目录下的所有日志文件rm -f *

  6. 去掉Myeclipse对JS等文件的验证

    在用Myeclipse导入新工程或在写代码时,最郁闷的就是它对js,jsp,html的验证太严格了,有时会呈现一个红叉或者一个黄色的感慨号,一运行就报Cannot return from outsid ...

  7. 集合ArrayList分析

    目录 ArrayList 描述 重要的对象 遍历使用 与Collection关系 ArrayList属性 扩展:什么是序列化 transient关键字解析 ArrayList构造方法 无参构造 int ...

  8. 网站启动,报编译错误:类型“ASP.global_asax”同时存在两个文件夹的问题

    CS0433: The type 'ASP.global_asax' exists in both 'c:\Windows\Microsoft.NET\Framework64\v4.0.30319\T ...

  9. [Leetcode] 第318题 最大单词长度乘积

    一.题目描述 给定一个字符串数组 words,找到 length(word[i]) * length(word[j]) 的最大值,并且这两个单词不含有公共字母.你可以认为每个单词只包含小写字母.如果不 ...

  10. AppScan工具介绍与安装

    本文仅供个人参考学习,如做商业用途,请购买正版,谢谢! 介绍 AppScan是IBM公司出的一款Web应用安全测试工具,采用黑盒测试的方式,可以扫描常见的web应用安全漏洞.其工作原理,首先是根据起始 ...