题目意思:用队列实现栈,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. Java---字节输入,文件操作,病毒制造,请谨慎运行!

    今天刚刚学了Java文件操作,跟着老师的思路,迫不及待的制造了这个小病毒. 用到的是一些小知识,很简单. 创建文件和文件夹,向文件中写入字节. 我已渐渐的爱上了编程!!! 下面附上完整代码: impo ...

  2. loadrunner11有效的license

    下载安装deletelicense.exe先1)退出程序,把下载文件中的lm70.dll和mlr5lprg.dll覆盖掉..\HP\LoadRunner\bin下的这两个文件 2)注意,win7的话一 ...

  3. Java多线程Thread.yield(),thread.join(), Thread.sleep(200),Object.wait(),Object.notify(),Object.notifyAll()的区别

    Thread.yield(),在某个线程里调用Thread.yield(),会使这个线程由正在运行的running状态转变为等待cpu时间片的runable状态.join()是Thread类的一个非s ...

  4. Unicode 字符集及UTF-8 UTF-16编码

    很久以前发在他处的一篇博文,今天翻出来重新整理了一下 Unicode 字符集 共分为 17 个平面(plane), 分别对应 U+xx0000 - U+xxFFFF 的 code points, 其中 ...

  5. python多线程机制

    Python中的线程从一开始就是操作系统的原生线程.而Python虚拟机也同样使用一个全局解释器锁(Global Interpreter Lock,GIL)来互斥线程多Python虚拟机的使用. GI ...

  6. Spring入门一

    一 简介 1.Spring为企业应用的开发提供了一个轻量级的解决方案,该解决方案包括:基于依赖注入的核心机制.基于AOP的声明式事务管理.与多种持久层技术 的整合,以及优秀的Web MVC框架等等.可 ...

  7. jquery常用见的正则表达式

    quickexpr = /^(?:[^<]*(<[ww]+>)[^>]*$|#([w-]+)$)/  (?:…)表示是一个非捕获型 [^<]表示是以"<& ...

  8. Linux散列表(一)——操作函数

    散列表(又名哈希表)仅仅需要一个包含单一指针的链表头.它是双向链表的变体.它不同于双链表——表头和结点使用相同的结构体——散列表对表头和结点有不同的定义.如下: struct hlist_head { ...

  9. Unity三种截屏方法(非自带API)

    者利用了三种表现形式: 1,选择截图路径的方法 2,直接截取截屏的方法 3,截取鼠标圈选区域. 上代码,: 第一种是调用.net的类库,需要引用System.Windows.Forms.dll,在As ...

  10. 给想上MIT的牛学生说几句

    [来信] 老师您好! 非常冒昧的来打搅您,仅仅是在学习上实在有些困惑才来向您求教一番. 我是计算机科学与技术的大一学生,我非常喜欢我自己的专业,可是学校里讲的东西太慢,太浅,所以我一般都是自学,我在自 ...