Topic:
Evaluate the value of an arithmetic expression in Reverse Polish Notation.
Valid operators is + , - , * , / . Each operand is an integer or another expression.
Some Examples:
["2", "1", "+", "3", "*")--((2 + 1) (3)-9 ["4", "", "5", "/", "+"], 4 + (13/5))
test instructions and analysis: give a string, the program calculates the inverse of the Polish-style results, traverse the expression, hit the operand into the stack, hit the operator from the top of the stack to remove two operands, and then the results of the calculation into the stack, the last stack of the only remaining operand is the result of the calculation. Note here that the string type determines whether the value is equal and needs to be used. Equals.
Code:
public class Solution {public int evalrpn (string[] tokens) { stack<integer> stack=new stack<> (); c4/>for (int i=0;i<tokens.length;i++) { String token=tokens[i]; if (token.equals ("+") | | Token.equals ("-") | | Token.equals ("*") | | Token.equals ("/")) { int op1=stack.pop (); int Op2=stack.pop (); if (token.equals ("+")) { stack.push (OP2+OP1); } if (Token.equals ("-")) { stack.push (OP2-OP1); } if (Token.equals ("*")) { stack.push (OP2*OP1); } if (Token.equals ("/")) { stack.push (OP2/OP1); } } else{ Stack.push (integer.parseint (token)); } } int Res=stack.peek (); System.out.println (res); return res; }}
[Leetcode] 150. Evaluate Reverse Polish Notation Java