国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

155. Min Stack

2019-11-08 03:01:55
字體:
來源:轉載
供稿:網友

題目

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. Example: MinStack minStack = new MinStack(); minStack.push(-2); minStack.push(0); minStack.push(-3); minStack.getMin(); –> Returns -3. minStack.pop(); minStack.top(); –> Returns 0. minStack.getMin(); –> Returns -2. Subscribe to see which companies asked this question.


思路

搞兩個stack,一個存正常的stack值,另一個存當前stack個數對應的最小值


代碼

class MinStack { //思路,兩個stack,一個存數據,一個存stack里有n個數據時候對應的最小值 stack<int> numStack; stack<int> minStack;public: void push(int x) { numStack.push(x); if(minStack.empty()) { minStack.push(x); } else { if(minStack.top() < x) { minStack.push(minStack.top()); } else { minStack.push(x); } } } void pop() { numStack.pop(); minStack.pop(); } int top() { return numStack.top(); } int getMin() { return minStack.top(); }};
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 安宁市| 寿阳县| 台南市| 郁南县| 略阳县| 车致| 镇沅| 江门市| 余干县| 文安县| 尉犁县| 招远市| 获嘉县| 外汇| 泌阳县| 乌兰县| 霞浦县| 正阳县| 罗山县| 化州市| 伊宁市| 澳门| 西盟| 清水河县| 大庆市| 犍为县| 大悟县| 尉犁县| 肥东县| 乌兰浩特市| 改则县| 曲阜市| 鹰潭市| 望奎县| 错那县| 临沭县| 临澧县| 新邵县| 南阳市| 阿拉善右旗| 潞城市|