当前位置: 首页 > news >正文

LeetCode Implement Stack using Queues

原题链接在这里:https://leetcode.com/problems/implement-stack-using-queues/

题目:

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

题解:

Implement Queue using Stacks相对应。

用一个queue来实现stack. push时从queue尾add进去, 然后rotate了que.size()-1次.

pop()时从queue首 poll 出一个. top()是 peek() 一下.

Time Complexity: push, O(n). pop, O(1). peek, O(1). empty, O(1).

Space: O(n), queue size.

AC java:

 1 public class MyStack {
 2     
 3     Queue<Integer> que;
 4     /** Initialize your data structure here. */
 5     public MyStack() {
 6         que = new LinkedList<Integer>();
 7     }
 8     
 9     /** Push element x onto stack. */
10     public void push(int x) {
11         que.offer(x);
12         for(int i = 0; i<que.size()-1; i++){
13             que.offer(que.poll());
14         }
15     }
16     
17     /** Removes the element on top of the stack and returns that element. */
18     public int pop() {
19         return que.poll();
20     }
21     
22     /** Get the top element. */
23     public int top() {
24         return que.peek();
25     }
26     
27     /** Returns whether the stack is empty. */
28     public boolean empty() {
29         return que.isEmpty();
30     }
31 }
32 
33 /**
34  * Your MyStack object will be instantiated and called as such:
35  * MyStack obj = new MyStack();
36  * obj.push(x);
37  * int param_2 = obj.pop();
38  * int param_3 = obj.top();
39  * boolean param_4 = obj.empty();
40  */

 

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/4825031.html

相关文章:

  • Web前端学习-第六课HTML篇
  • C# 跨线程呼叫控制
  • (13)[Xamarin.Android] 不同分辨率下的图片使用概论
  • Unity3d之流光效果
  • mysql 的 存储结构(储存引擎)
  • DP_ural_Metro
  • 手把手教你整合 SpringMvc+Spring+MyBatis+Maven
  • oracle根据pid查询出正在执行的执行语句
  • 国内最简单的短视频SDK
  • 【转】vxworks的default boot line说明
  • vector的reserve和resize(转)
  • 心跳多少寿命长
  • UI中的界面之间的值传递 一
  • [POJ3067]Japan
  • 将数据集导出到Excel
  • JavaScript 如何正确处理 Unicode 编码问题!
  • 2017前端实习生面试总结
  • 2019年如何成为全栈工程师?
  • ERLANG 网工修炼笔记 ---- UDP
  • es6(二):字符串的扩展
  • extjs4学习之配置
  • mysql 5.6 原生Online DDL解析
  • ReactNative开发常用的三方模块
  • Sass 快速入门教程
  • Travix是如何部署应用程序到Kubernetes上的
  • 百度小程序遇到的问题
  • 从PHP迁移至Golang - 基础篇
  • 得到一个数组中任意X个元素的所有组合 即C(n,m)
  • 关于extract.autodesk.io的一些说明
  • 离散点最小(凸)包围边界查找
  • 浏览器缓存机制分析
  • 设计模式(12)迭代器模式(讲解+应用)
  • 说说我为什么看好Spring Cloud Alibaba
  • #数学建模# 线性规划问题的Matlab求解
  • $ is not function   和JQUERY 命名 冲突的解说 Jquer问题 (
  • (1)(1.8) MSP(MultiWii 串行协议)(4.1 版)
  • (poj1.2.1)1970(筛选法模拟)
  • (翻译)Entity Framework技巧系列之七 - Tip 26 – 28
  • (使用vite搭建vue3项目(vite + vue3 + vue router + pinia + element plus))
  • (五)网络优化与超参数选择--九五小庞
  • (一)Linux+Windows下安装ffmpeg
  • (一)UDP基本编程步骤
  • (转)【Hibernate总结系列】使用举例
  • .dwp和.webpart的区别
  • .h头文件 .lib动态链接库文件 .dll 动态链接库
  • .NET Core 版本不支持的问题
  • .net core 源码_ASP.NET Core之Identity源码学习
  • .NET 设计一套高性能的弱事件机制
  • .net 验证控件和javaScript的冲突问题
  • .NET 中 GetProcess 相关方法的性能
  • .Net多线程总结
  • .NET设计模式(2):单件模式(Singleton Pattern)
  • .set 数据导入matlab,设置变量导入选项 - MATLAB setvaropts - MathWorks 中国
  • /dev下添加设备节点的方法步骤(通过device_create)
  • [ C++ ] STL priority_queue(优先级队列)使用及其底层模拟实现,容器适配器,deque(双端队列)原理了解