C#LeetCode刷题之#232-用栈实现队列(Implement Queue using Stacks)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4108 访问。
使用栈实现队列的下列操作:
push(x) -- 将一个元素放入队列的尾部。
pop() -- 从队列首部移除元素。
peek() -- 返回队列首部的元素。
empty() -- 返回队列是否为空。
MyQueue queue = new MyQueue();
queue.push(1);
queue.push(2);
queue.peek(); // 返回 1
queue.pop(); // 返回 1
queue.empty(); // 返回 false
说明:
你只能使用标准的栈操作 -- 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)。
Implement the following operations of a queue using stacks.
push(x) -- Push element x to the back of queue.
pop() -- Removes the element from in front of queue.
peek() -- Get the front element.
empty() -- Return whether the queue is empty.
MyQueue queue = new MyQueue();
queue.push(1);
queue.push(2);
queue.peek(); // returns 1
queue.pop(); // returns 1
queue.empty(); // returns false
Notes:
You must use only standard operations of a stack -- which means only push to top, peek/pop from top, size, and is empty operations are valid.
Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4108 访问。
public class Program {
public static void Main(string[] args) {
var queue = new MyQueue();
queue.Push(1);
queue.Push(2);
queue.Push(3);
Console.WriteLine(queue.Peek());
Console.WriteLine(queue.Pop());
Console.WriteLine(queue.Empty());
Console.ReadKey();
}
public class MyQueue {
private Stack<int> _stack = null;
public MyQueue() {
_stack = new Stack<int>();
}
public void Push(int x) {
//基本思路是反转原栈
var stack = new Stack<int>();
stack.Push(x);
var reverse = _stack.Reverse().ToList();
foreach(var elemet in reverse) {
stack.Push(elemet);
}
_stack = stack;
}
public int Pop() {
return _stack.Pop();
}
public int Peek() {
return _stack.Peek();
}
public bool Empty() {
return _stack.Count == 0;
}
}
}
以上给出1种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4108 访问。
1
1
False
分析:
显而易见,因为部分运行库的使用,Push 的时间复杂度应当为: ,其它方法的时间复杂度应当为:
。
C#LeetCode刷题之#232-用栈实现队列(Implement Queue using Stacks)的更多相关文章
- LeetCode 232. 用栈实现队列(Implement Queue using Stacks) 4
232. 用栈实现队列 232. Implement Queue using Stacks 题目描述 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从 ...
- LeetCode 232:用栈实现队列 Implement Queue using Stacks
题目: 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从队列首部移除元素. peek() -- 返回队列首部的元素. empty() -- 返回队列是 ...
- [Swift]LeetCode232. 用栈实现队列 | Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- LeetCode 刷题笔记 155. 最小栈(Min Stack)
tag: 栈(stack) 题目描述 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- 删除栈顶的元素 ...
- C#LeetCode刷题之#155-最小栈(Min Stack)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4020 访问. 设计一个支持 push,pop,top 操作,并能 ...
- C#LeetCode刷题-栈
栈篇 # 题名 刷题 通过率 难度 20 有效的括号 C#LeetCode刷题之#20-有效的括号(Valid Parentheses) 33.0% 简单 42 接雨水 35.6% 困难 71 简 ...
- C#LeetCode刷题-设计
设计篇 # 题名 刷题 通过率 难度 146 LRU缓存机制 33.1% 困难 155 最小栈 C#LeetCode刷题之#155-最小栈(Min Stack) 44.9% 简单 173 二叉搜索 ...
- LeetCode刷题 --杂篇 --数组,链表,栈,队列
武汉加油,中国加油.希望疫情早日结束. 由于疫情,二狗寒假在家不能到处乱逛,索性就在家里系统的刷一下算法的内容,一段时间下来倒也有些小小的收获.只是一来家中的小破笔记本写起博客来实在不是很顺手,二来家 ...
- leetcode刷题记录——栈和队列
题目 232.用栈实现队列 class MyQueue { private Stack<Integer> in = new Stack<>(); private Stack&l ...
- LeetCode刷题专栏第一篇--思维导图&时间安排
昨天是元宵节,过完元宵节相当于这个年正式过完了.不知道大家有没有投入继续投入紧张的学习工作中.年前我想开一个Leetcode刷题专栏,于是发了一个投票想了解大家的需求征集意见.投票于2019年2月1日 ...
随机推荐
- P1136 迎接仪式 题解
题目描述 LHX教主要来X市指导OI学习工作了.为了迎接教主,在一条道路旁,一群Orz教主er穿着文化衫站在道路两旁迎接教主,每件文化衫上都印着大字.一旁的Orzer依次摆出"欢迎欢迎欢迎欢 ...
- SSM框架前后端信息交互
一.从前端向后端传送数据 常见的3种方式 1.form表单的action:此方法可以提交form表单内的输入数据,也可同时提交某些隐藏但设置有默认值的<input>,如修改问题时,我们除了 ...
- system.out.println从什么方向执行
从左向右执行,当“+”号前后有字符串出现时,就当做字符串连接符号处理了.
- Cyber Security - Palo Alto Firewall Objects Addresses, Services, and Groups(2)
Users Objects and Groups Creating local user objects. Creating local user groups. https://docs.paloa ...
- PyQt5多线程和定时器
多线程 一般情况单线程就可以很好的完成任务,但是对于GUI程序来说,单线程就不能完全满足需求.如果有耗时流程,在单线程的情况下,界面操作就会卡死,直到耗时操作完成,才会响应界面操作.为了解决这个问题, ...
- C++语法小记---重载逗号操作符
重载逗号操作符 逗号操作符算法:从左到右依次计算每一个表达式的值,整个逗号表达式的值等于最右边表达式的值,前面n-1个表达式可以没有返回值 重载逗号操作符: 参数必须有一个class成员 重载函数返回 ...
- 惠普EX920强打760P驱动后无法开机,无法启动系统。
Windows10 1909与Intel 760P驱动版本不匹配的原因,造成了强打驱动后,无法启动系统的原因. 惠普EX920强打Intel 760P驱动教程:http://tieba.baidu.c ...
- Go语言基础语法总结
1. 认识HelloWorld 在前面的<Go的安装和使用>这篇文章中已经写过HelloWorld.go了,现在就来逐行认识一下它. package main import "f ...
- R语言基本绘图-plot参数:标题,坐标轴和颜色
标题 plot(c(1:2,2:4),main = "这是主标题",sub = "这是副标题",xlab = "这是x轴", ylab = ...
- PD快充和QC快充
参考链接:https://zhidao.baidu.com/question/246420719602653564.html PD快充协议 PD即USB Power Delivery Specific ...