博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Leetcode 155: Mini Stack
阅读量:4150 次
发布时间:2019-05-25

本文共 750 字,大约阅读时间需要 2 分钟。

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.
class MinStack {public:       void push(int x) {         elements.push(x);          if (mins.empty()||x<=mins.top())              mins.push(x);      }       void pop() {         if (elements.empty()) return;         if (elements.top() == mins.top())             mins.pop();         elements.pop();     }      int top() {         return elements.top();     }      int getMin() {         return mins.top();     } private:     stack
elements; stack
mins;};

转载地址:http://hyxti.baihongyu.com/

你可能感兴趣的文章
VUe+webpack构建单页router应用(一)
查看>>
(python版)《剑指Offer》JZ01:二维数组中的查找
查看>>
Spring MVC中使用Thymeleaf模板引擎
查看>>
深入了解php底层机制
查看>>
PHP中的stdClass 【转】
查看>>
XHProf-php轻量级的性能分析工具
查看>>
OpenCV gpu模块样例注释:video_reader.cpp
查看>>
就在昨天,全球 42 亿 IPv4 地址宣告耗尽!
查看>>
Mysql复制表以及复制数据库
查看>>
Linux分区方案
查看>>
如何使用 systemd 中的定时器
查看>>
linux进程监控和自动重启的简单实现
查看>>
OpenFeign学习(三):OpenFeign配置生成代理对象
查看>>
OpenFeign学习(四):OpenFeign的方法同步请求执行
查看>>
OpenFeign学习(六):OpenFign进行表单提交参数或传输文件
查看>>
Ribbon 学习(二):Spring Cloud Ribbon 加载配置原理
查看>>
Ribbon 学习(三):RestTemplate 请求负载流程解析
查看>>
深入理解HashMap
查看>>
XML生成(一):DOM生成XML
查看>>
XML生成(三):JDOM生成
查看>>