题目意思:用队列实现栈,push(),pop(),top(),empty()

思路:用两个queue,pop时将一个queue的元素pop再push到另一个队列,queue只留最后一个元素,并pop,再将目标队列变为另一个

  ps:用栈实现队列,参考剑指offer

 class Stack {
private:
queue<int> q[];
int flag=;
public:
// Push element x onto stack.
void push(int x) {
q[flag].push(x);
} // Removes the element on top of the stack.
void pop() {
while(q[flag].size()>){
q[-flag].push(q[flag].front());
q[flag].pop();
}
q[flag].pop();
flag=-flag;
} // Get the top element.
int top() {
return q[flag].back();
} // Return whether the stack is empty.
bool empty() {
if(q[flag].empty()){
return true;
}
return false;
}
};

225 Implement Stack using Queues(用队列实现栈Medium)的更多相关文章

  1. LeetCode 225 Implement Stack using Queues 用队列实现栈

    1.两个队列实现,始终保持一个队列为空即可 class MyStack { public: /** Initialize your data structure here. */ MyStack() ...

  2. [LeetCode] 225. Implement Stack using Queues 用队列来实现栈

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  3. LeetCode OJ:Implement Stack using Queues(队列实现栈)

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  4. Implement Stack using Queues 用队列实现栈

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  5. leetcode 155. Min Stack 、232. Implement Queue using Stacks 、225. Implement Stack using Queues

    155. Min Stack class MinStack { public: /** initialize your data structure here. */ MinStack() { } v ...

  6. 232. Implement Queue using Stacks,225. Implement Stack using Queues

    232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...

  7. LeetCode 225 Implement Stack using Queues(用队列来实现栈)(*)

    翻译 用队列来实现栈的例如以下操作. push(x) -- 将元素x加入进栈 pop() -- 从栈顶移除元素 top() -- 返回栈顶元素 empty() -- 返回栈是否为空 注意: 你必须使用 ...

  8. 225 Implement Stack using Queues 队列实现栈

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack.pop ...

  9. [LeetCode] Implement Stack using Queues 用队列来实现栈

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

随机推荐

  1. -_-#【CSS3】浏览器前缀

    前缀 浏览器 -khtml Konqueror(非常老的 Safari) -moz Firefox -o Opera -ms Internet Explorer -webkit Safari.Chro ...

  2. 贪心:SPOJ Backup Files

    BACKUP - Backup Files no tags  You run an IT company that backs up computer data for large offices. ...

  3. 【搜索】【并查集】Codeforces 691D Swaps in Permutation

    题目链接: http://codeforces.com/problemset/problem/691/D 题目大意: 给一个1到N的排列,M个操作(1<=N,M<=106),每个操作可以交 ...

  4. 【模拟】Codeforces 691C Exponential notation

    题目链接: http://codeforces.com/problemset/problem/691/C 题目大意: 输入一个数,把它表示成a·10b形式(aEb).输出aEb,1<=a< ...

  5. Merge into(oracle)

    作用:使用一条sql语句进行insert或者update操作,如果存在就update,如果不存在就insert 语法: MERGE INTO table_name t1 USING (table|vi ...

  6. 《精通CSS:高级Web标准解决方案》学习笔记(上)

    鉴于国产CSS书籍基本都是辣鸡的现状,我在半年前动用某工作室的购书资金采购了一些技术书籍,这本广受好评的<精通CSS>也在其中.但是在阅读过后我深深的感觉到,如果说CSS本来已经是一种很琐 ...

  7. UVa 11796 计算几何

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  8. [转载]10 Best Tools For Websites And Apps Development Ever

    转载自: http://www.websurfmedia.com/10-best-tools-for-websites-and-apps-development-ever/   The world i ...

  9. webstrom开发微信小程序说明

    在操作之前,需要对webstrom做一些设置,如下 如果未安装node.js的朋友,请到如下地址 https://nodejs.org/en/ 安装(相信大家都是会的),如果安装完了之后,就使用如下的 ...

  10. HDU--2040

    亲和数 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submis ...