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

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

LeetCode225—Implement Stack using Queues

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

原題鏈接

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).

分析

要讓隊首的元素到對尾去,因為stack是先進后出,沒次入隊之后將隊首元素再入隊。

class MyStack { queue<int>q;public: /** Initialize your data structure here. */ MyStack() { } /** Push element x onto stack. */ void push(int x) { q.push(x); for(int i=0;i<q.size()-1;++i) { q.push(q.front()); q.pop(); } } /** Removes the element on top of the stack and returns that element. */ int pop() { int v=q.front(); q.pop(); return v; } /** Get the top element. */ int top() { return q.front(); } /** Returns whether the stack is empty. */ bool empty() { return q.empty(); }};
上一篇:SSL 2352_面積_bfs

下一篇:UVa-11388 GCD LCM

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 斗六市| 于田县| 阳曲县| 揭西县| 天长市| 博客| 镇平县| 宁城县| 临朐县| 遵义县| 宁海县| 平谷区| 历史| 江津市| 新丰县| 德庆县| 渝北区| 石渠县| 荆门市| 大安市| 和静县| 河南省| 布尔津县| 白城市| 萍乡市| 临潭县| 常州市| 灵宝市| 进贤县| 扶余县| 和龙市| 太仆寺旗| 澄江县| 江津市| 万荣县| 固原市| 永顺县| 禄丰县| 兴海县| 永登县| 丘北县|