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 backpeek/pop from frontsize, 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).

Update (2015-06-11):
The class name of the Java function had been updated to MyStack instead of Stack.

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and all test cases.

用一个队列就可实现,看了很多例子觉得discuss里面有个朋友写的很简练,就用了他的丝路,和她基本一致

 class Stack {
public:
// Push element x onto stack..
queue<int> a;
void push(int x) {
a.push(x);
} // Removes the element on top of the stack.
void pop() {
size_t i = ;
size_t size = a.size();
for(i = ; i < size-; ++i)
{
a.push(a.front());
a.pop();
}
a.pop();
} // Get the top element.
int top() {
return a.back();
} // Return whether the stack is empty.
bool empty() {
return a.empty();
}
};

(leetcode)Implement Stack using Queues的更多相关文章

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

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

  2. LeetCode——Implement Stack using Queues

    Description: Implement the following operations of a stack using queues. push(x) -- Push element x o ...

  3. LeetCode Implement Stack using Queues (数据结构)

    题意: 用队列来实现栈. 思路: 没有什么捷径,纯粹模拟.但是用一个队列就够了. class Stack { /* // Push element x onto stack. void push(in ...

  4. leetcode:Implement Stack using Queues 与 Implement Queue using Stacks

    一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...

  5. 【LeetCode】232 & 225 - Implement Queue using Stacks & Implement Stack using Queues

    232 - Implement Queue using Stacks Implement the following operations of a queue using stacks. push( ...

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

  7. lc面试准备:Implement Stack using Queues

    1 题目 Implement the following operations of a stack using queues. push(x) -- Push element x onto stac ...

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

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

  9. Implement Queue by Two Stacks & Implement Stack using Queues

    Implement Queue by Two Stacks Implement the following operations of a queue using stacks. push(x) -- ...

随机推荐

  1. SpringMVC、Struts1、Struts2和SSH2框架中单例与多例的解析

    struts1是单例提供服务,请求数据绑定在actionform里,form当做参数传入调用方法,不用实例变量就不会出现并发问题. spring mvc的contrller也是这样的模式 struts ...

  2. Qt make clickable label 制作可点击的Label控件

    Qt中的Label标签控件的作用绝不仅仅限于显示静态文本,其实它的功能很强大,由于其有setPixmap()成员函数,故而可以当显示图片窗口使用,而且还可以实时显示摄像头捕获的图片,然后它对鼠标事件的 ...

  3. 分布式架构高可用架构篇_03-redis3集群的安装高可用测试

    参考文档 Redis 官方集群指南:http://redis.io/topics/cluster-tutorial Redis 官方集群规范:http://redis.io/topics/cluste ...

  4. ionic 写一个五星评价(非指令)

    Controller里的代码: .controller('evaluateCtrl', function($scope, $state, $stateParams, $ionicPopup,$ioni ...

  5. HTML5 :b/strong加粗,i/em倾斜区别

    解释1 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="ut ...

  6. redmine问题

    问题1: 404 版本库中不存在该条目和(或)其修订版本. 1.1 GIT库: 参考:http://stackoverflow.com/questions/13000247/redmine-gitol ...

  7. 2016.07.14,英语,《Vocabulary Builder》Unit 25

    verb: comes from the Latin verbum, meaning 'word'. verbally: ['vɜːbəli] adv. 口头地,词句地, 逐字地 verbalize: ...

  8. [故障处理]联想笔记本故障0x0000007B

    同事笔记本故障,莫名其妙的快捷方式就找不到了.开始程序中的内容也无法正常查看. 解决步骤: 1.怀疑用户配置的问题,新建一个用户,没有解决. 2.使用自带的一键恢复ThinkVantage,恢复后,重 ...

  9. Nginx 笔记与总结(6)Location:精准匹配

    在 /usr/local/nginx/conf/nginx.conf 的 server 段中,location 表示根据 URI 来进行不同的定位:把网站的不同部分定位到不同的处理方式上,例如遇到 . ...

  10. http响应报文和http请求报文 详细信息

    tomcat项目本身的jar包