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

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. 解决subline安装插件被墙失败的方法

    一.问题场景描述 当你完成subline和package control的安装后,准备使用install package安装各种各样的插件来丰富你的编辑器,却出现类似 “Unable to downl ...

  2. TypeScript泛型

    泛型的概念 指不预先确定的数据类型,具体的类型要在使用的时候才能确定.咋一听,是不是觉得JavaScript本身就是这样?这是由于理解有误.前面说“在使用的时候确定”,而非在程序执行的时候确定. 泛型 ...

  3. HTML5为什么只需要写<!DOCTYPE HTML>?

    HTML4.01中的doctype需要对DTD进行引用,因为HTML4.01基于SGML.而HTML5不基于SGML,因此不需要对DTD进行引用,但是需要doctype来规范浏览器的行为.其中,SGM ...

  4. Spring Boot应用上传文件时报错

    问题描述 Spring Boot应用(使用默认的嵌入式Tomcat)在上传文件时,偶尔会出现上传失败的情况,后台报错日志信息如下:"The temporary upload location ...

  5. Python机器学习笔记:卷积神经网络最终笔记

    这已经是我的第四篇博客学习卷积神经网络了.之前的文章分别是: 1,Keras深度学习之卷积神经网络(CNN),这是开始学习Keras,了解到CNN,其实不懂的还是有点多,当然第一次笔记主要是给自己心中 ...

  6. TestNG(十二) 参数化测试

    <?xml version="1.0" encoding="utf-8" ?> <suite name = "parameter&q ...

  7. charles 代理设置

    本文参考:charles 代理设置 charles的代理身份配置区域 这里应该没啥好说的了, window/MAXCos proxy 是charles的代理启用开关: 如果你选中,则表示电脑使用cha ...

  8. Java连载34-对象的内存分析、对象之间建立关系

    一.内存分析 代码:引用可以是局部变量也可以是成员变量 public class Test1{ public static void main(String[] args){ User u = new ...

  9. Python3.7.4入门-3函数

    3 函数 3.1 定义函数 def fib(n): # write Fibonacci series up to n """Print a Fibonacci serie ...

  10. ASP.NET Core 3.0 : 二十四. 配置的Options模式

    上一章讲到了配置的用法及内部处理机制,对于配置,ASP.NET Core还提供了一种Options模式.(ASP.NET Core 系列目录) 一.Options的使用 上一章有个配置的绑定的例子,可 ...