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

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

class MyStack {

    List<Integer> stack = new ArrayList<>();

    // Push element x onto stack.
public void push(int x) {
stack.add(x);
} // Removes the element on top of the stack.
public void pop() {
if(!empty()){
stack.remove(stack.size()-1);
}
} // Get the top element.
public int top() {
if(!empty()){
return stack.get(stack.size()-1);
}
return -1;
} // Return whether the stack is empty.
public boolean empty() {
return stack.size()==0;
}
}

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. java.lang.Exception: Socket bind failed: [730048] ?????????×???(Э?é/???????/???)????í??

    严重: Error starting endpoint java.lang.Exception: Socket bind failed: [730048] ?????????×???(Э?é/???? ...

  2. Oracle实用技巧

    一. ORACLE SQL PLUS 使用技巧: ----①查找重复记录: SELECT DRAWING, DSNOFROM EM5_PIPE_PREFABWHERE ROWID!= (SELECT ...

  3. Oracle dblink 使用详解

    1.dblink简介 dblink(Database Link)数据库链接就是数据库的链接,跨本地数据库 2.使用语法详解 基本语法 CREATE [SHARED][PUBLIC] database ...

  4. Objective-C中的分类与协议

    分类 在谈分类之前,我们可以先探究下,OC中为什么出现分类这种机制,有什么好处? 假设你接到一个大项目:计算两个整数的和,差.接到任务的你马上动手.编写代码如下: #import <Founda ...

  5. Servlet(三)

    重定向 服务器向浏览器发送一个302状态码以及一个Location消息头(该消息头的值是一个地址,称之为重定向地址),浏览器收到后会立即向重定向的地址发出请求,使用相应对象的API方法实现(respo ...

  6. Spring中的创建与销毁

    在bean中添加属性init-method="方法名" destroy-method="方法名" init-method        该方法是由spring容 ...

  7. Fedora 21 安装Infinality

    原文地址: Fedora 21 用infinality美化你的字体 http://blog.csdn.net/element207/article/details/41746683 安装infinal ...

  8. Chrome rem bug

    遇到一个bug,发现chrome在初始化页面的时候,会错误的渲染rem单位,导致字体过大. 比如: 正常的应该是这样的: 原因是,为了使用rem单位,我们常常将 html 的font-size设置为6 ...

  9. php之购物车类思路及代码

    <?php /* 购物车类 1.整站范围内,购物车--全局有效 解决:把购物车的信息,放在session里 2.既然全局有效,购物车的实例只有一个 解决:单例模式 技术选型:session+单例 ...

  10. Linux系统分区

    在Linux系统里面,"分区",被称作"挂载点" 挂载点 意思就是把一部分硬盘容量,分成一个文件夹的形式,用来做某些事情,这个文件夹的名字,就叫做:挂载点 (如 ...