From 66ea6b80f4d61939466f91e4ec5be4c193485fc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D1=8B=D1=82=D0=BA=D0=BE=D0=B2=20=D0=A0=D0=BE=D0=BC?= =?UTF-8?q?=D0=B0=D0=BD?= Date: Tue, 30 Jan 2024 23:36:49 +0300 Subject: [PATCH] 225. Implement Stack using Queues --- .../225. Implement Stack using Queues.csproj | 11 ++++ 225. Implement Stack using Queues/MyStack.cs | 50 +++++++++++++++++++ 225. Implement Stack using Queues/Program.cs | 3 ++ Leetcode.sln | 6 +++ 4 files changed, 70 insertions(+) create mode 100644 225. Implement Stack using Queues/225. Implement Stack using Queues.csproj create mode 100644 225. Implement Stack using Queues/MyStack.cs create mode 100644 225. Implement Stack using Queues/Program.cs diff --git a/225. Implement Stack using Queues/225. Implement Stack using Queues.csproj b/225. Implement Stack using Queues/225. Implement Stack using Queues.csproj new file mode 100644 index 0000000..d8e54c8 --- /dev/null +++ b/225. Implement Stack using Queues/225. Implement Stack using Queues.csproj @@ -0,0 +1,11 @@ + + + + Exe + net8.0 + _225._Implement_Stack_using_Queues + enable + enable + + + diff --git a/225. Implement Stack using Queues/MyStack.cs b/225. Implement Stack using Queues/MyStack.cs new file mode 100644 index 0000000..3373761 --- /dev/null +++ b/225. Implement Stack using Queues/MyStack.cs @@ -0,0 +1,50 @@ +namespace _225._Implement_Stack_using_Queues; + +public class MyStack +{ + private Queue _queue = new Queue(256); + private int? _lastNum = null; + + public MyStack() + { + } + + public void Push(int x) + { + if (_lastNum == null) + { + _lastNum = x; + return; + } + + int count = _queue.Count; + _queue.Enqueue(_lastNum.Value); + _lastNum = x; + while (count-- > 0) + _queue.Enqueue(_queue.Dequeue()); + } + + public int Pop() + { + if (_lastNum != null) + { + int res = _lastNum.Value; + _lastNum = null; + return res; + } + + return _queue.Dequeue(); + } + + public int Top() + { + if (_lastNum != null) + return _lastNum.Value; + return _queue.Peek(); + } + + public bool Empty() + { + return _lastNum == null && _queue.Count == 0; + } +} \ No newline at end of file diff --git a/225. Implement Stack using Queues/Program.cs b/225. Implement Stack using Queues/Program.cs new file mode 100644 index 0000000..e5dff12 --- /dev/null +++ b/225. Implement Stack using Queues/Program.cs @@ -0,0 +1,3 @@ +// See https://aka.ms/new-console-template for more information + +Console.WriteLine("Hello, World!"); \ No newline at end of file diff --git a/Leetcode.sln b/Leetcode.sln index 5007126..a7f1be6 100644 --- a/Leetcode.sln +++ b/Leetcode.sln @@ -163,6 +163,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "8. String to Integer (atoi) EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "15. 3Sum", "15. 3Sum\15. 3Sum.csproj", "{4FAFB0B1-A240-44B9-8305-3CA7FED8AE3C}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "225. Implement Stack using Queues", "225. Implement Stack using Queues\225. Implement Stack using Queues.csproj", "{4DF0467F-C3C9-4F92-9EE7-D21AD30F559D}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -489,6 +491,10 @@ Global {4FAFB0B1-A240-44B9-8305-3CA7FED8AE3C}.Debug|Any CPU.Build.0 = Debug|Any CPU {4FAFB0B1-A240-44B9-8305-3CA7FED8AE3C}.Release|Any CPU.ActiveCfg = Release|Any CPU {4FAFB0B1-A240-44B9-8305-3CA7FED8AE3C}.Release|Any CPU.Build.0 = Release|Any CPU + {4DF0467F-C3C9-4F92-9EE7-D21AD30F559D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4DF0467F-C3C9-4F92-9EE7-D21AD30F559D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4DF0467F-C3C9-4F92-9EE7-D21AD30F559D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4DF0467F-C3C9-4F92-9EE7-D21AD30F559D}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE