LeetCode_155-Min Stack
栈的实现,多加了一个最小值的获取
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的更多相关文章
- [LintCode] Min Stack 最小栈
Implement a stack with min() function, which will return the smallest number in the stack. It should ...
- [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 ...
- leetcode 155. Min Stack --------- java
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- Min Stack [LeetCode 155]
1- 问题描述 Design a stack that supports push, pop, top, and retrieving the minimum element in constant ...
- Min Stack
Min Stack Design a stack that supports push, pop, top, and retrieving the minimum element in constan ...
- Java [Leetcode 155]Min Stack
题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant ti ...
- 155. Min Stack
题目: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time ...
- leetCode Min Stack解决共享
原标题:https://oj.leetcode.com/problems/min-stack/ Design a stack that supports push, pop, top, and ret ...
- LeetCode算法题-Min Stack(Java实现)
这是悦乐书的第177次更新,第179篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第36题(顺位题号是155).设计一个支持push,pop,top和在恒定时间内检索最小 ...
- 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 ...
随机推荐
- FreeSql (十四)批量更新数据
FreeSql支持丰富的更新数据方法,支持单条或批量更新,在特定的数据库执行还可以返回更新后的记录值. var connstr = "Data Source=127.0.0.1;Port=3 ...
- rabbitmqctl: Error unable to connect to node 'rabbit@xxxxx' nodedown
RabbitMQ使用rabbitmqctl add_user的时候报以下错误: Creating user "xxxx" ... Error: unable to connect ...
- Java-HashSet集合中的几种遍历方式
//我们先创建一个set集合 public static void main(String[] args) { Set<Integer> sets = new HashSet<> ...
- 松软科技课堂:SQL-LEFT-JOIN 关键字
SQL LEFT JOIN 关键字 LEFT JOIN 关键字会从左表 (table_name1) 那里返回所有的行,即使在右表 (table_name2) 中没有匹配的行. LEFT JOIN 关键 ...
- FourAndSix2 靶机渗透
0x01 简介 FourAndSix2是易受攻击的一个靶机,主要任务是通过入侵进入到目标靶机系统然后提权,并在root目录中并读取flag.tx信息 FourAndSix2.镜像下载地址: https ...
- Hadoop 之 HDFS的使用
1.列出hadoop下面的目录, hadoop fs -ls / 进入user hadoop fs ls /user 2.再usr/root下新建input目录 hadoop -mkdir inpu ...
- C++基础之动态内存
C++支持动态分配对象,它的生命周期与它们在哪里创建无关,只有当显示的被释放时,这些对象才会被销毁.分配在静态或栈内存中的对象由编译器自动创建和销毁. new在动态内存中为对象分配空间并返回一个指向该 ...
- Codeforces Numbers 题解
这题只需要会10转P进制就行了. PS:答案需要约分,可以直接用c++自带函数__gcd(x,y). 洛谷网址 Codeforces网址 Code(C++): #include<bits/std ...
- MySQL性能优化以及常用命令
1.将查询操作SELECT中WHERE条件后面和排序字段建立索引 2.按需查询,需要哪个字段就查哪个字段,禁止使用"SELECT * " 3.数据库引擎最好选用InnoDB,少用M ...
- 关于大脑与CPU的简单思考
今天午休突发奇想的思考了大脑与cpu的差异,发现出了大脑是生物信号驱动的单核cpu而已(并行任务是时间片的调度,要额外的堆栈记忆或者物理如纸张的存储). 大脑永远是线性的逐行执行指令,执行期间无法判断 ...