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))
classSolution { Public: intEVALRPN (vector<string> &tokens) {Stack<int>Nums; intnum =tokens.size (); intI=0; if(num = =1)returnTonum (tokens[0]); while(i<num) { while(I<num &&!)Isop (Tokens[i])) { intTMP =Tonum (Tokens[i]); Nums.push (TMP); I++; } intOP2 =Nums.top (); Nums.pop (); intOP1 =Nums.top (); Nums.pop (); if(Tokens[i] = ="+") {Nums.push (OP1+OP2); } Else if(Tokens[i] = ="*") {Nums.push (OP1*OP2); } Else if(Tokens[i] = ="-") {Nums.push (OP1-OP2); } Else if(Tokens[i] = ="/") {Nums.push (OP1/OP2); } I++; } returnNums.top (); } intTonum (strings) { intnum; StringStream SS (s); SS>>num; returnnum; } BOOLIsopstringstr) { if(Str.length () >1)return false; if(str = ="+")return true; if(str = ="*")return true; if(str = ="-")return true; if(str = ="/")return true; }};
Evaluate Reverse Polish Notation