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

题目大意:用队列,实现栈。

  1. class MyStack {
  2.  
  3. List<Integer> stack = new ArrayList<>();
  4.  
  5. // Push element x onto stack.
  6. public void push(int x) {
  7. stack.add(x);
  8. }
  9.  
  10. // Removes the element on top of the stack.
  11. public void pop() {
  12. if(!empty()){
  13. stack.remove(stack.size()-1);
  14. }
  15. }
  16.  
  17. // Get the top element.
  18. public int top() {
  19. if(!empty()){
  20. return stack.get(stack.size()-1);
  21. }
  22. return -1;
  23. }
  24.  
  25. // Return whether the stack is empty.
  26. public boolean empty() {
  27. return stack.size()==0;
  28. }
  29. }

Implement Stack using Queues ——LeetCode的更多相关文章

  1. Implement Stack using Queues leetcode

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

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

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

  3. 【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( ...

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

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

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

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

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

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

  8. 【一天一道LeetCode】#225. Implement Stack using Queues

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Impleme ...

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

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

随机推荐

  1. SQL Server 表字段值转换成字段名称(二)

    上次写了个比较简单的只有两个字段的例子,经要求在写个  3 个字段的示例 ,贴上来与大家共勉一下   如果你们有更好的方法,提供一下, 感激不尽. 示例如下: /*--drop table temp_ ...

  2. sql数值显示成千分位分隔符的形式

    ), )--带小数点 ), ),'.00','')--不带小数点

  3. youphp学习整理

    <?php //后台公共模块 // _list 数据显示 // add 添加/编辑 视图 // insert 添加处理函数 // edit 添加/编辑 视图 // update 更新处理函数 / ...

  4. 写个接口的实现类,在方法的前面加了@Override居然报错

    据说这是jdk的问题,@Override是JDK5就已经有了,但有个小小的Bug,就是不支持对接口的实现,认为这不是Override 而JDK6修正了这个Bug,无论是对父类的方法覆盖还是对接口的实现 ...

  5. 用户组,AD域控简介

    “自由”的工作组    工作组(WORK GROUP)就是将不同的电脑按功能分别列入不同的组中,以方便管理.比如在一个网络内,可能有成百上千台工作电脑,如果这些电脑不进行分组,都列在“网上邻居”内,可 ...

  6. c# web 删除时弹出提示框

    方法1: 在控件中增加属性 <asp:Button ID="btnSub" runat="server" Text="提交" oncl ...

  7. What is SaaS?

    SaaS, or Software as a Service, describes any cloud service where consumers are able to access softw ...

  8. sublime text 几种常用插件

    1.docblockr  //文档注释 使用 /**  +tab 在函数前就可以   2.SublimeLinter 代码校验插件,支持多种语言,这个是主插件,如果想检测特定的文件需要单独下载   3 ...

  9. Linux下JDK环境变量配置

    JDK官方下载地址: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 我的下载路 ...

  10. 基于ProGuard-Maven-Plugin的自定义代码混淆插件

    介绍 大家可能都会碰到一些代码比较敏感的项目场景,这个时候代码被反编译看到就不好了,这个时候就需要代码混淆插件来对代码进行混淆了. 基于Maven的项目一般会去考虑使用proguard-maven-p ...