From 93ab9befe85fdd3056765b3414b8b442a7f2258d Mon Sep 17 00:00:00 2001 From: Chenxue3 <115330251+XueshanChen@users.noreply.github.com> Date: Sat, 6 Jan 2024 23:51:02 +1300 Subject: [PATCH] =?UTF-8?q?Update=200225.=E7=94=A8=E9=98=9F=E5=88=97?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E6=A0=88.md=20=E5=A2=9E=E5=8A=A0C#=E5=8D=95?= =?UTF-8?q?=E9=98=9F=E5=88=97=E5=AE=9E=E7=8E=B0=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0225.用队列实现栈.md | 51 +++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/problems/0225.用队列实现栈.md b/problems/0225.用队列实现栈.md index d89bf44b..6900e668 100644 --- a/problems/0225.用队列实现栈.md +++ b/problems/0225.用队列实现栈.md @@ -1046,6 +1046,8 @@ class MyStack() { ### C#: +> 双队列 + ```csharp public class MyStack { Queue queue1; @@ -1080,6 +1082,54 @@ public class MyStack { } ``` +> 单队列 + +```c# +/* + * @lc app=leetcode id=225 lang=csharp + * 版本二:单队列 + * [225] Implement Stack using Queues + */ + +// @lc code=start +public class MyStack { + Queue myQueue; + public MyStack() { + myQueue = new Queue(); + } + + public void Push(int x) { + myQueue.Enqueue(x); + } + + //使用一个队列实现 + public int Pop() { + //一个队列在模拟栈弹出元素的时候只要将队列头部的元素(除了最后一个元素外) 重新添加到队列尾部,此时再去弹出元素就是栈的顺序了。 + for (var i = 0; i < myQueue.Count-1; i++) + { + myQueue.Enqueue(myQueue.Dequeue()); + } + return myQueue.Dequeue(); + } + + //复用Pop()的代码 + public int Top() { + int res = Pop(); + myQueue.Enqueue(res); + return res; + } + + public bool Empty() { + return (myQueue.Count == 0); + } +} + +// @lc code=end + +``` + + + ### PHP: > 双队列 @@ -1203,3 +1253,4 @@ impl MyStack { +