Evaluate Reverse Polish Notation
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))
https://leetcode.com/problems/evaluate-reverse-polish-notation/
Inverse Polish expression, or suffix expression, after two more difficult questions, this second.
Http://www.cnblogs.com/Liok3187/p/4564877.html
Http://www.cnblogs.com/Liok3187/p/4593365.html
1 /**2 * @param {string[]} tokens3 * @return {number}4 */5 varEVALRPN =function(tokens) {6 varResstack = [];7 varOpstack = [];8 varRight , left;9 for(vari = 0; i < tokens.length; i++){Ten varCurr =Tokens[i]; One if(/^(\+|\-|\/|\*)$/. Test (Curr)) { Aright =Resstack.pop (); -left =Resstack.pop (); - if(Curr = = = ' + ')){ theResstack.push (left +Right ); -}Else if(Curr = = = '-')){ -Resstack.push (left-Right ); -}Else if(Curr = = = '/')){ +Resstack.push (parseint (left/Right )); -}Else if(Curr = = = ' * ')){ +Resstack.push (Left *Right ); A } at}Else{ - Resstack.push (parseint (Curr)); - } - } - returnResstack[0]; -};
[leetcode][javascript]evaluate Reverse Polish Notation