A Javascript solution


  • 0
    D
     function op(leftNum,rightNum,op){
    	if(op === "+")
    		return leftNum + rightNum;
    	else if(op === "-")
    		return leftNum - rightNum;
    	else if(op === "*")
    		return leftNum * rightNum;
    	else if(op === "/")
    		return leftNum / rightNum;
    }
    var evalRPN = function(tokens) {
        var stack = [];
        var top = -1;
        var left,right,oper;
        var ops = ["+","-","*","/"];
        for(var i = 0,len = tokens.length;i < len; ++i){
        	stack.push(tokens[i]);
        	++top;
        	if(ops.indexOf(tokens[i]) !== -1 && top >= 2){
        		oper = stack.pop();
        		right = parseInt(stack.pop());
        		left = parseInt(stack.pop());
        		stack.push(parseInt(op(left,right,oper)));
        		top -= 2;
        	}
        }
        return(parseInt(stack.pop()));
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.