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. C#Socket编程socket.Connect权限出错问题及解决

    最近使用Vs2010编写Socket程序,客户端在调用socket.Connect()时,总是出现: 请求“System.Net.SocketPermission, System, Version=4 ...

  2. Activity Threa创建Window和View分析

    http://blog.csdn.net/ljsbuct/article/details/7094580 1. 入口. 以前一直都说Activity的人口是onCreate方法.其实android上一 ...

  3. Java方法-数组

    [Java数组] 1. 用sort()方法对Java数组进行排序,及如何使用 binarySearch() 方法来查找数组中的元素 binarySearch() 返回值: 如果它包含在数组中,则返回搜 ...

  4. ITEXTSHARP学习整理

    学习的版本iTextSharp.5.5.5. 关于获取PDF中的图片资源 /// <summary> /// 将PDF中的图片资源转换成二进制 /// </summary> / ...

  5. MATLAB中mexFunction函数的接口规范

    MEX文件的调用极为方便,其调用方式与MATALAB的内建函数完全相同,只需要在命令窗口内输入对应的文件名称即可. C语言MEX程序代码文件有计算子例程(Computational routine)和 ...

  6. 优秀Android开源项目

    开源项目汇总: Trinea/android-open-project · GitHub 包含个性化控件.工具库.优秀项目.开发及测试工具等 优秀完整项目: 1.Google I/O Android ...

  7. C# 窗体靠近屏幕边缘自动隐藏*学习(类似于QQ)

    using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; us ...

  8. rel=nofollow 是什么意思

    nofollow是什么意思? nofollow是html标签的一个属性值,Google推荐使用nofollow,告诉机器(爬虫)无需追踪目标页,是指禁止蜘蛛爬行和传递权重,但是如果你是通过sitema ...

  9. 4种检测是否支持HTML5的方法,你知道几个?

    4种检测是否支持HTML5的方法,你知道几个? 1,检查特定的属性是否存在于全局的对象里面,比如说window或navigator. 比如geolocation,它是HTML5新加支持的新特性:它是由 ...

  10. IIS10 设置支持wcf服务(.svc)

    感谢: http://www.cnblogs.com/dudu/p/3328066.html 如果提示web.config配置重复的话,很有可能是.net framework版本的问题,把IIS中的版 ...