Min Stack
来源:互联网 发布:电脑多开软件 编辑:程序博客网 时间:2024/06/06 00:40
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
- push(x) -- Push element x onto stack.
- pop() -- Removes the element on top of the stack.
- top() -- Get the top element.
- getMin() -- Retrieve the minimum element in the stack.
设计一个堆支持push,pop,top和瞬间检索最小元素的功能。
学过数据结构就很简单了,而且c++专门有stack结构,代码如下:
class MinStack {private: stack<int> content; stack<int> mins; public: void push(int x) { content.push(x); if(mins.empty() || x <= mins.top()) mins.push(x); } void pop() { int top = content.top(); content.pop(); if(top == mins.top()) { mins.pop(); } } int top() { return content.top(); } int getMin() { return mins.top(); } };
0 0
- min stack
- Min Stack
- Min Stack
- Min Stack
- Min Stack
- Min Stack
- Min Stack
- Min Stack
- Min Stack
- Min Stack
- Min Stack
- Min Stack
- Min Stack
- Min Stack
- Min Stack
- Min Stack
- Min Stack
- Min Stack
- JavaScript 在各个浏览器中执行的耐性
- 内存泄露
- Sicily 1268. Single Digit Adder
- Qt 学习笔记
- IOS开发—多线程之NSOperation介绍
- Min Stack
- MongoDB 安装及测试
- Sicily 1904. Travel
- linux mysql 安装
- 微信公众平台开发(98) UnionID
- windows注册服务,“1057:账户名无效或不存在”错误
- Socket编程实践(6) --TCP服务端注意事项
- Opengl渲染管线 坐标变换 纹理与光照
- NFS