From 9d50f345d1661cfe6ff774ff5469721db94541a0 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: Wed, 31 Jan 2024 00:49:39 +0300 Subject: [PATCH] 150. Evaluate Reverse Polish Notation --- .idea/.idea.Leetcode/.idea/workspace.xml | 30 +++++++++++++++--- ...0. Evaluate Reverse Polish Notation.csproj | 11 +++++++ .../Program.cs | 11 +++++++ .../Solution.cs | 31 +++++++++++++++++++ Leetcode.sln | 6 ++++ 5 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 150. Evaluate Reverse Polish Notation/150. Evaluate Reverse Polish Notation.csproj create mode 100644 150. Evaluate Reverse Polish Notation/Program.cs create mode 100644 150. Evaluate Reverse Polish Notation/Solution.cs diff --git a/.idea/.idea.Leetcode/.idea/workspace.xml b/.idea/.idea.Leetcode/.idea/workspace.xml index 0c61c7b..34f7850 100644 --- a/.idea/.idea.Leetcode/.idea/workspace.xml +++ b/.idea/.idea.Leetcode/.idea/workspace.xml @@ -88,9 +88,9 @@ - - - + + + @@ -127,6 +127,7 @@ - + + + + @@ -1642,7 +1662,7 @@ 1678030074860 - + diff --git a/150. Evaluate Reverse Polish Notation/150. Evaluate Reverse Polish Notation.csproj b/150. Evaluate Reverse Polish Notation/150. Evaluate Reverse Polish Notation.csproj new file mode 100644 index 0000000..2eb755f --- /dev/null +++ b/150. Evaluate Reverse Polish Notation/150. Evaluate Reverse Polish Notation.csproj @@ -0,0 +1,11 @@ + + + + Exe + net8.0 + _150._Evaluate_Reverse_Polish_Notation + enable + enable + + + diff --git a/150. Evaluate Reverse Polish Notation/Program.cs b/150. Evaluate Reverse Polish Notation/Program.cs new file mode 100644 index 0000000..6df972f --- /dev/null +++ b/150. Evaluate Reverse Polish Notation/Program.cs @@ -0,0 +1,11 @@ +// See https://aka.ms/new-console-template for more information + +using _150._Evaluate_Reverse_Polish_Notation; + +string[] arr = ["10", "1", "1", "2", "+", "+", "+"]; +var sol = new Solution(); +int n = 100000; +while(n-- > 0) + Console.WriteLine(sol.EvalRPN(["10", "1", "1", "2", "+", "+", "+"])); + + \ No newline at end of file diff --git a/150. Evaluate Reverse Polish Notation/Solution.cs b/150. Evaluate Reverse Polish Notation/Solution.cs new file mode 100644 index 0000000..6c66880 --- /dev/null +++ b/150. Evaluate Reverse Polish Notation/Solution.cs @@ -0,0 +1,31 @@ +namespace _150._Evaluate_Reverse_Polish_Notation; + +public class Solution +{ + public int EvalRPN(string[] tokens) + { + var stack = new Stack(tokens.Length); + foreach (var token in tokens) + { + if (int.TryParse(token, out var number)) + stack.Push(number); + else + { + int right = stack.Pop(); + int left = stack.Pop(); + char op = token[0]; + stack.Push(Calc(op, left, right)); + } + } + return stack.Pop(); + } + + private int Calc(char op, int left, int right) => op switch + { + '+' => left + right, + '-' => left - right, + '*' => left * right, + '/' => left / right, + _ => throw new ArgumentOutOfRangeException() + }; +} \ No newline at end of file diff --git a/Leetcode.sln b/Leetcode.sln index b20c052..06bdebe 100644 --- a/Leetcode.sln +++ b/Leetcode.sln @@ -167,6 +167,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "225. Implement Stack using EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "14. Longest Common Prefix", "14. Longest Common Prefix\14. Longest Common Prefix.csproj", "{E1C1A088-861F-4E6D-A3D4-631C3B207A5F}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "150. Evaluate Reverse Polish Notation", "150. Evaluate Reverse Polish Notation\150. Evaluate Reverse Polish Notation.csproj", "{10A95EED-FD1D-40E7-B9E0-2DD48BE05AB9}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -501,6 +503,10 @@ Global {E1C1A088-861F-4E6D-A3D4-631C3B207A5F}.Debug|Any CPU.Build.0 = Debug|Any CPU {E1C1A088-861F-4E6D-A3D4-631C3B207A5F}.Release|Any CPU.ActiveCfg = Release|Any CPU {E1C1A088-861F-4E6D-A3D4-631C3B207A5F}.Release|Any CPU.Build.0 = Release|Any CPU + {10A95EED-FD1D-40E7-B9E0-2DD48BE05AB9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {10A95EED-FD1D-40E7-B9E0-2DD48BE05AB9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {10A95EED-FD1D-40E7-B9E0-2DD48BE05AB9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {10A95EED-FD1D-40E7-B9E0-2DD48BE05AB9}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE