topic
Add a link description
Problem-solving ideas
- The former through the array backwards
- Digital encounter is pushed onto the stack
- The first two characters meet the top of the stack will be calculated symbol calculation result together with the stack
- Through the array in the end, the top element is the final answer
public class Solution {
public int EvalRPN(string[] tokens) {
Stack<int> data = new Stack<int>();
for (int i = 0; i < tokens.Length; i++)
{
if (tokens[i] == "+" || tokens[i] == "/" || tokens[i] == "-" || tokens[i] == "*")
{
int a = data.Pop();
int b = data.Pop();
if (tokens[i] == "+")
{
data.Push(b + a);
}
if (tokens[i] == "-")
{
data.Push(b - a);
}
if (tokens[i] == "*")
{
data.Push(b * a);
}
if (tokens[i] == "/")
{
data.Push(b / a);
}
}
else
data.Push(int.Parse(tokens[i]));
}
return data.Pop();
}
}