题目意思:用队列实现栈,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. module_init和init_module的区别

    今天在看CS8900的驱动时,发现其驱动的模块加载函数是init_module(),由于看到大多数的驱动用的模块加载函数大多是module_init()函数,所以一时没缓过神来,总是在找CS8900的 ...

  2. (转载)PHP源代码分析- tick(s)

    (转载)http://bbs.phpchina.com/forum.php?mod=viewthread&tid=94534 昨天有位朋友在杭州的PHPer群里面贴出了下面的一段代码并给出了运 ...

  3. iOS Block中的weakSelf/strongSelf

    Objective C 的 Block 是一个很实用的语法,特别是与GCD结合使用,可以很方便地实现并发.异步任务.但是,如果使用不当,Block 也会引起一些循环引用问题(retain cycle) ...

  4. Drawer_layout 关闭滑动视图

    在android抽屉Drawer_layout开发中,我需要关闭滑动的试图 找到了这个方法 mDrawer_layout.setDrawerLockMode(DrawerLayout.LOCK_MOD ...

  5. 使用jqMobi开发app基础:Badge的使用

    显示效果: 红色的部分就是Badge,能够用来显示数量或者是其它的信息. 使用事实上非常easy,  $.ui.updateBadge("#" + id, res.Msg, &qu ...

  6. Android之开发常用颜色

    Android开发中常常要用一些个性化的颜色,然而茫茫的RBG颜色对照表,往往给人眼花缭乱的感觉,更别说从中轻易选出一两种比较满意的颜色,下面我就总结一下开发中常用到的比较绚丽的颜色,都是有名有姓的哦 ...

  7. linux查看cpu温度

      分类: linux系统 一.安装  sudo apt-get install lm-sensors   二.查看 linux@cdyemail:~$ sensors k10temp-pci-00c ...

  8. 第三篇:数据仓库系统的实现与使用(含OLAP重点讲解)

    前言 上一篇重点讲解了数据仓库建模,它是数据仓库开发中最核心的部分.然而完整的数据仓库系统还会涉及其他一些组件的开发,其中最主要的是ETL工程,在线分析处理工具(OLAP)和商务智能(BI)应用等. ...

  9. 《CODE》讲了什么?

    本书首先从黑夜中用手电筒开关灯的方式来与小伙伴交流从而引出了编码与组合的概念,并阐明了编码的本质就是交流,是一种用来在机器与人之间传递信息的方式.然后在第 2~3 章中讲述了编码与组合的应用,如电报机 ...

  10. Android中使用HttpGet和HttpPost访问HTTP资源

    需求:用户登录(name:用户名,pwd:密码) (一)HttpGet :doGet()方法//doGet():将参数的键值对附加在url后面来传递 public String getResultFo ...