麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

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

155. Min Stack / 225. Implement Stack using Queues

2019-11-10 19:55:51
字體:
來源:轉載
供稿:網友

Min Stack題目描述代碼實現Implement Stack using Queues題目描述代碼實現

155. Min Stack

題目描述

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.

代碼實現

使用vector來模擬:

class MinStack {public: vector<int> stk; /** initialize your data structure here. */ MinStack() { } void push(int x) { stk.push_back(x); } void pop() { stk.pop_back(); } int top() { return stk.back(); } int getMin() { int min_val = INT_MAX; int s_len = stk.size(); for(int i = 0; i < s_len; i++) { if(stk[i] < min_val) min_val = stk[i]; } return min_val; }};/** * Your MinStack object will be instantiated and called as such: * MinStack obj = new MinStack(); * obj.push(x); * obj.pop(); * int param_3 = obj.top(); * int param_4 = obj.getMin(); */

使用兩個棧來模擬:

class MinStack {PRivate: stack<int> s1; stack<int> s2;public: void push(int x) { s1.push(x); if (s2.empty() || x <= getMin()) s2.push(x); } void pop() { if (s1.top() == getMin()) s2.pop(); s1.pop(); } int top() { return s1.top(); } int getMin() { return s2.top(); }};

225. Implement Stack using Queues

題目描述

Implement the following Operations of a stack using queues.

push(x) – Push element x onto stack.pop() – Removes the element on top of the stack.top() – Get the top element.empty() – Return whether the stack is empty.

Notes:

You must use only standard operations of a queue – which means only push to back, peek/pop from front, size, and is empty operations are valid.Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).

代碼實現

實現方法一:

class MyStack {private: queue<int> q1;public: /** Initialize your data structure here. */ MyStack() { } /** Push element x onto stack. */ void push(int x) { q1.push(x); } /** Removes the element on top of the stack and returns that element. */ int pop() { int len = q1.size(); for(int i = 0; i < len - 1; i++) { int tmp = q1.front(); q1.pop(); q1.push(tmp); } int res = q1.front(); q1.pop(); return res; } /** Get the top element. */ int top() { return q1.back(); } /** Returns whether the stack is empty. */ bool empty() { return !q1.size()?true:false; }};/** * Your MyStack object will be instantiated and called as such: * MyStack obj = new MyStack(); * obj.push(x); * int param_2 = obj.pop(); * int param_3 = obj.top(); * bool param_4 = obj.empty(); */

當然還有另外一種思路,就是使用在push的時候進行排序:

class MyStack {public: queue<int> que; // Push element x onto stack. void push(int x) { que.push(x); for(int i=0;i<que.size()-1;++i){ que.push(que.front()); que.pop(); } } // Removes the element on top of the stack. int pop() { int res = que.front(); que.pop(); return res; } // Get the top element. int top() { return que.front(); } // Return whether the stack is empty. bool empty() { return que.empty(); }};
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 女人叉开腿让男人桶 | 亚洲性在线视频 | 黄色片网站免费在线观看 | 国产一区二区三区精品在线观看 | 亚洲成人第一区 | 国产精品视频一区二区三区四区国 | 黄色美女网站免费看 | 亚洲综合视频在线播放 | 久久久久久久久久久久久国产精品 | 成人在线a| 久久久久久久不卡 | 成人在线观看免费爱爱 | 天天干天天碰 | 红桃一区 | 欧美视频一区二区 | 狠狠操天天射 | 中文字幕一二三区芒果 | 一级毛片播放 | 欧美日韩在线视频一区 | 中国女警察一级毛片视频 | 全网免费毛片 | 国产精品一区在线观看 | 久久艹逼 | 亚洲一级成人 | 久久久久北条麻妃免费看 | 久久精品一区二区三区不卡牛牛 | 免费看毛片的网站 | 欧美高清视频一区 | 国产亚洲精品久久午夜玫瑰园 | 正在播放91 | 欧美日韩国产成人在线 | 国产精品99久久久久久久vr | 国产69精品久久久久孕妇黑 | 欧美日韩一区,二区,三区,久久精品 | 青青草成人影视 | 国产精品久久久久久久久久东京 | 91成人午夜性a一级毛片 | 精品国产一区二区三区久久久 | 青草av.久久免费一区 | 欧美精品一区二区性色 | julieann艳星激情办公室 |