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

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. spring组件注册

    基于注解和类的组件注册 @Conditional 作用:按照一定的条件进行判断,如果满足条件的话就给spring容器中注册bean ​ 该注解既可以标注到方法上面,也可以标注到类上面(只有满足条件时, ...

  2. mybatis 批量插入时候的一个注意点

    <insert id="batchInsert" parameterType="java.util.List" useGeneratedKeys=&quo ...

  3. Go语言标准库之context

    在 Go http包的Server中,每一个请求在都有一个对应的 goroutine 去处理.请求处理函数通常会启动额外的 goroutine 用来访问后端服务,比如数据库和RPC服务.用来处理一个请 ...

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

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

  5. ASP.NET Core SignalR:集线器Hubs

    一.什么是集线器hubs 通过SignalR的集线器hubs中定义的方法,服务器可以调用连接中的客户端定义的方法,而客户端也可以调用服务器端集线器中定义的方法.SignalR负责实现了客户端和服务器之 ...

  6. Net基础篇_学习笔记_第十天_方法(函数)

    方法(函数): 函数就是将一堆代码进行重用的一种机制.//解决冗余代码问题------方法出现了.            面向对象的三大特征:封装.继承.多态 函数的语法:[public] stati ...

  7. Qt无边框窗体-模拟模态窗体抖动效果

    目录 一.概述 二.效果展示 三.功能实现 四.相关文章 原文链接:Qt无边框窗体-模拟模态窗体抖动效果 一.概述 用Qt开发windows客户端界面确实是一大利器,兼顾性能的同时,速度相对来说也不错 ...

  8. FEDay会后-Serverless与云开发,可能是前端的下一站

    进化本身是生物体与环境之间持续不断的信息交换的具体表现. -- 摘自<信息简史> 很荣幸在9月21号成都举办的第五届FEDay上作为讲师为大家分享腾讯云在近两年推出的云开发相关的技术和知识 ...

  9. Docker学习之docker-compose

    docker-compose 安装 1.Mac/Windows: 安装docker的时候附带安装了. 2.Linux: curl https://github.com/docker/compose L ...

  10. RDD基本操作之Action

    Action介绍 在RDD上计算出来一个结果 把结果返回给driver program或保存在文件系统,count(),save 常用的Action reduce() 接收一个函数,作用在RDD两个类 ...