150. Evaluate Reverse Polish Notation

This commit is contained in:
Пытков Роман
2024-01-31 00:49:39 +03:00
parent 1e43adf29e
commit 9d50f345d1
5 changed files with 84 additions and 5 deletions

View File

@@ -88,9 +88,9 @@
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="11f2de1c-1ee5-4aa4-91e6-11975ebd421f" name="Changes" comment=""> <list default="true" id="11f2de1c-1ee5-4aa4-91e6-11975ebd421f" name="Changes" comment="">
<change afterPath="$PROJECT_DIR$/14. Longest Common Prefix/14. Longest Common Prefix.csproj" afterDir="false" /> <change afterPath="$PROJECT_DIR$/150. Evaluate Reverse Polish Notation/150. Evaluate Reverse Polish Notation.csproj" afterDir="false" />
<change afterPath="$PROJECT_DIR$/14. Longest Common Prefix/Program.cs" afterDir="false" /> <change afterPath="$PROJECT_DIR$/150. Evaluate Reverse Polish Notation/Program.cs" afterDir="false" />
<change afterPath="$PROJECT_DIR$/14. Longest Common Prefix/Solution.cs" afterDir="false" /> <change afterPath="$PROJECT_DIR$/150. Evaluate Reverse Polish Notation/Solution.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/.idea.Leetcode/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/.idea.Leetcode/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/.idea.Leetcode/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/.idea.Leetcode/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Leetcode.sln" beforeDir="false" afterPath="$PROJECT_DIR$/Leetcode.sln" afterDir="false" /> <change beforePath="$PROJECT_DIR$/Leetcode.sln" beforeDir="false" afterPath="$PROJECT_DIR$/Leetcode.sln" afterDir="false" />
</list> </list>
@@ -127,6 +127,7 @@
<component name="PropertiesComponent"><![CDATA[{ <component name="PropertiesComponent"><![CDATA[{
"keyToString": { "keyToString": {
".NET Project.14. Longest Common Prefix.executor": "Run", ".NET Project.14. Longest Common Prefix.executor": "Run",
".NET Project.150. Evaluate Reverse Polish Notation.executor": "Run",
"ASKED_ADD_EXTERNAL_FILES": "true", "ASKED_ADD_EXTERNAL_FILES": "true",
"RunOnceActivity.OpenProjectViewOnStart": "true", "RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true", "RunOnceActivity.ShowReadmeOnStart": "true",
@@ -149,7 +150,7 @@
] ]
} }
}]]></component> }]]></component>
<component name="RunManager" selected=".NET Project.14. Longest Common Prefix"> <component name="RunManager" selected=".NET Project.150. Evaluate Reverse Polish Notation">
<configuration name="101. Symmetric Tree" type="DotNetProject" factoryName=".NET Project"> <configuration name="101. Symmetric Tree" type="DotNetProject" factoryName=".NET Project">
<option name="EXE_PATH" value="" /> <option name="EXE_PATH" value="" />
<option name="PROGRAM_PARAMETERS" value="" /> <option name="PROGRAM_PARAMETERS" value="" />
@@ -402,6 +403,24 @@
<option name="Build" /> <option name="Build" />
</method> </method>
</configuration> </configuration>
<configuration name="150. Evaluate Reverse Polish Notation" type="DotNetProject" factoryName=".NET Project" temporary="true">
<option name="EXE_PATH" value="$PROJECT_DIR$/150. Evaluate Reverse Polish Notation/bin/Debug/net8.0/150. Evaluate Reverse Polish Notation" />
<option name="PROGRAM_PARAMETERS" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/150. Evaluate Reverse Polish Notation/bin/Debug/net8.0" />
<option name="PASS_PARENT_ENVS" value="1" />
<option name="USE_EXTERNAL_CONSOLE" value="0" />
<option name="USE_MONO" value="0" />
<option name="RUNTIME_ARGUMENTS" value="" />
<option name="PROJECT_PATH" value="$PROJECT_DIR$/150. Evaluate Reverse Polish Notation/150. Evaluate Reverse Polish Notation.csproj" />
<option name="PROJECT_EXE_PATH_TRACKING" value="1" />
<option name="PROJECT_ARGUMENTS_TRACKING" value="1" />
<option name="PROJECT_WORKING_DIRECTORY_TRACKING" value="1" />
<option name="PROJECT_KIND" value="DotNetCore" />
<option name="PROJECT_TFM" value="net8.0" />
<method v="2">
<option name="Build" />
</method>
</configuration>
<configuration name="15. 3Sum" type="DotNetProject" factoryName=".NET Project"> <configuration name="15. 3Sum" type="DotNetProject" factoryName=".NET Project">
<option name="EXE_PATH" value="" /> <option name="EXE_PATH" value="" />
<option name="PROGRAM_PARAMETERS" value="" /> <option name="PROGRAM_PARAMETERS" value="" />
@@ -1628,6 +1647,7 @@
</configuration> </configuration>
<recent_temporary> <recent_temporary>
<list> <list>
<item itemvalue=".NET Project.150. Evaluate Reverse Polish Notation" />
<item itemvalue=".NET Project.14. Longest Common Prefix" /> <item itemvalue=".NET Project.14. Longest Common Prefix" />
</list> </list>
</recent_temporary> </recent_temporary>
@@ -1642,7 +1662,7 @@
<updated>1678030074860</updated> <updated>1678030074860</updated>
<workItem from="1678030078340" duration="114000" /> <workItem from="1678030078340" duration="114000" />
<workItem from="1680804946367" duration="3697000" /> <workItem from="1680804946367" duration="3697000" />
<workItem from="1706644472303" duration="3045000" /> <workItem from="1706644472303" duration="6672000" />
</task> </task>
<servers /> <servers />
</component> </component>

View File

@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>_150._Evaluate_Reverse_Polish_Notation</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -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", "+", "+", "+"]));

View File

@@ -0,0 +1,31 @@
namespace _150._Evaluate_Reverse_Polish_Notation;
public class Solution
{
public int EvalRPN(string[] tokens)
{
var stack = new Stack<int>(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()
};
}

View File

@@ -167,6 +167,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "225. Implement Stack using
EndProject 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}" 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 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 Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU 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}.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.ActiveCfg = Release|Any CPU
{E1C1A088-861F-4E6D-A3D4-631C3B207A5F}.Release|Any CPU.Build.0 = 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 EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE