Title Link: evaluate-reverse-polish-notation
Import java.util.stack;/** * Evaluate The value of a arithmetic expression in Reverse Polish notation.valid operators is +, -, *, /. Each operand is an integer or another expression. Some examples: ["2", "1", "+", "3", "*"), 1 ["3", "+", "9", "/", "+"] (4 + (13/5))-&G T 6 * */public class Evaluatereversepolishnotation {//20/20 test Cases passed.//status:accepted//runtime:346 Ms//Submitt ed:0 minutes ago//time complexity O (n) space complexity O (n) public int evalrpn (string[] tokens) {stack<integer> Stack = NE W stack<integer> (); for (String s:tokens) {if (IsOp (s)) {Stack.push (Operation (Stack.pop (), S, Stack.pop ()));} else {Stack.push (Stringtoint ( s));}} return Stack.pop (); }//Determines whether the operator public boolean isOp (String s) {if (S.equals ("+") | | S.equals ("-") | | S.equals ("*") | | S.equals ("/")) {return true; } else {return false;} }//Operator operation on two numbers public int operation (int b, String s, int a) {IF (s.equals ("+")) return a + B; else if (s.equals ("-")) return a-B; else if (s.equals ("*")) return a * b; else return a/b; }//Converts a numeric string into a digital public int stringtoint (string s) {int n = 0; int flag = 1; For (Character C:s.tochararray ()) {//determine if the negative if (c = = '-') {flag = -1;continue;} n = ten * n + C-' 0 ';} return flag * N; }public static void Main (string[] args) {//TODO auto-generated method stub}}
[Leetcode 150] Evaluate Reverse Polish Notation