[LeetCode][JavaScript]Evaluate Reverse Polish Notation

Evaluate Reverse Polish Notation

Evaluate the value of an arithmetic expression in Reverse Polish Notation.

Valid operators are +-*/. Each operand may be an integer or another expression.

Some examples:

  ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
  ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6

https://leetcode.com/problems/evaluate-reverse-polish-notation/



逆波兰表达式,也就是后缀表达式,做过两道更难的题之后,这题秒了。

http://www.cnblogs.com/Liok3187/p/4564877.html

http://www.cnblogs.com/Liok3187/p/4593365.html

 1 /**
 2  * @param {string[]} tokens
 3  * @return {number}
 4  */
 5 var evalRPN = function(tokens) {
 6     var resStack = [];
 7     var opStack = [];
 8     var right, left;
 9     for(var i  = 0; i < tokens.length; i++){
10         var curr = tokens[i];
11         if(/^(\+|\-|\/|\*)$/.test(curr)){
12             right = resStack.pop();
13             left = resStack.pop();
14             if(curr === ‘+‘){
15                 resStack.push(left + right);
16             }else if(curr === ‘-‘){
17                 resStack.push(left - right);
18             }else if(curr === ‘/‘){
19                 resStack.push(parseInt(left / right));
20             }else if(curr === ‘*‘){
21                 resStack.push(left * right);
22             }
23         }else{
24             resStack.push(parseInt(curr));
25         }
26     }
27     return resStack[0];
28 };
时间: 2024-08-02 07:01:23

[LeetCode][JavaScript]Evaluate Reverse Polish Notation的相关文章

【LeetCode】Evaluate Reverse Polish Notation

题目 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["2", "1", "+", "3", "*"]

leetcode - [2]Evaluate Reverse Polish Notation

Evaluate Reverse Polish Notation Total Accepted: 24595 Total Submissions: 123794My Submissions Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expres

【Leetcode】Evaluate Reverse Polish Notation答案

一.原题 Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["2", "1", "+&qu

【Leetcode】Evaluate Reverse Polish Notation JAVA

   一.问题描述 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["2", "1", "+", "3", "*&

leetcode 150. Evaluate Reverse Polish Notation ------ java

Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["2", "1", "+", "3", "*"] -&g

[Leetcode][JAVA] Evaluate Reverse Polish Notation

Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["2", "1", "+", "3", "*"] -&g

[C++]LeetCode: 98 Evaluate Reverse Polish Notation

题目: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["2", "1", "+", "3", "*"]

【leetcode】Evaluate Reverse Polish Notation(middle)

Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["2", "1", "+", "3", "*"] -&g

LeetCode OJ - Evaluate Reverse Polish Notation

题目: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["2", "1", "+", "3", "*"]