[LC] 225. 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.
Example:
MyStack stack = new MyStack(); stack.push(1);
stack.push(2);
stack.top(); // returns 2
stack.pop(); // returns 2
stack.empty(); // returns false
class MyStack: def __init__(self):
"""
Initialize your data structure here.
"""
from collections import deque
self.queue = deque() def push(self, x: int) -> None:
"""
Push element x onto stack.
"""
self.queue.append(x)
for i in range(len(self.queue) - 1):
self.queue.append(self.queue.popleft()) def pop(self) -> int:
"""
Removes the element on top of the stack and returns that element.
"""
return self.queue.popleft() def top(self) -> int:
"""
Get the top element.
"""
return self.queue[0] def empty(self) -> bool:
"""
Returns whether the stack is empty.
"""
return not self.queue # Your MyStack object will be instantiated and called as such:
# obj = MyStack()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.top()
# param_4 = obj.empty()
class MyStack { /** Initialize your data structure here. */
Queue<Integer> queue;
public MyStack() {
queue = new LinkedList<>();
} /** Push element x onto stack. */
public void push(int x) {
queue.offer(x);
for (int i = 0; i < queue.size() - 1; i++) {
queue.offer(queue.poll());
}
} /** Removes the element on top of the stack and returns that element. */
public int pop() {
return queue.poll();
} /** Get the top element. */
public int top() {
return queue.peek();
} /** Returns whether the stack is empty. */
public boolean empty() {
return queue.isEmpty();
}
} /**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* boolean param_4 = obj.empty();
*/
[LC] 225. Implement Stack using Queues的更多相关文章
- 232. Implement Queue using Stacks,225. Implement Stack using Queues
232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...
- 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 ...
- Java for LeetCode 225 Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- (easy)LeetCode 225.Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- Leetcode 225 Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- Java [Leetcode 225]Implement Stack using Queues
题目描述: Implement the following operations of a stack using queues. push(x) -- Push element x onto sta ...
- 【LeetCode】225. Implement Stack using Queues
题目: Implement the following operations of a stack using queues. push(x) -- Push element x onto stack ...
- 【一天一道LeetCode】#225. Implement Stack using Queues
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Impleme ...
- LeetCode 225 Implement Stack using Queues(用队列来实现栈)(*)
翻译 用队列来实现栈的例如以下操作. push(x) -- 将元素x加入进栈 pop() -- 从栈顶移除元素 top() -- 返回栈顶元素 empty() -- 返回栈是否为空 注意: 你必须使用 ...
随机推荐
- 使用linux服务器安装wordpress博客详细教程
前言 最近读了<软技能:代码之外的生存指南>,这本书给了我很大的启示.之前虽然知道作为一个程序员,应该拥有自己的博客,以便于提升自己的知名度,但是并没有了解的过于详细.这本书描写博客的作用 ...
- Paper Review: Epigenetic Landscape, Cell Differentiation 01
Today, I'll share a review papers about Epigenetic Landscape, the Epigenetic Landscape is related to ...
- CodeForces 1292A NEKO's Maze Game(思维)
#include <stdio.h> #include <string.h> #include <iostream> #include <string> ...
- liunx 常用操作(自用)
Centos7解压文件 tar -zxvf 文件名[test.tar.gz] Centos7安装vim yum -y install vim* Centos7安装ifconfig yum instal ...
- 63)对于STL基本概念东西 自己百度(没有整理)
基础知识 看 C++进阶课程讲义的那个word文档
- nginx中rewrite flag
rewrite 正则表达式 新URI [flag]; [flag] 选项用于调控重写的行为,它的取值可能是: last:重写完成后,会停止继续处理当前区块所有属于ngx_http_rewrite ...
- 题解 P1884 【[USACO12FEB]过度种植(银)Overplanting 】
什么,扫描线需要线段树? 那我第一个不干啊(其实是不会写) 这里介绍一种裸的扫描线: 我们根据x排序,对于相等的 \(x\) ,将 \(y\) 进入和退出分类讨论,然后全部放进set里面.每次 \(x ...
- Python dict 和 list 转换
这里有个dict d1 = { 'en':'英语', 'cn':'中文', 'fr':'法语', 'jp':'日语' } 使用d1.keys()或 d1.values() 可以提取出values 和k ...
- 注册登录页面修订-Python使用redis-手机验证接口-发送短信验证
登录页面修订 views.Login.vue <template> <div class="login box"> <img src="@/ ...
- 测试Java程序执行耗费的时间
package test; public class Main { public static void main(String[] args) { long start = System.curre ...