LeetcodeOJ: Evaluate Reverse Polish Notation Stack

 1 #define ADDITION  ‘+‘
 2 #define SUBSTRACTION  ‘-‘
 3 #define MULTIPLICATION  ‘*‘
 4 #define DIVISION  ‘/‘
 5
 6
 7 class Solution {
 8 public:
 9     set<char> tokenSet{‘+‘, ‘-‘, ‘*‘, ‘/‘};
10     stack<int> num;
11     int evalRPN(vector<string> &tokens) {
12         for (auto &token : tokens) {
13             auto iter = tokenSet.find( token[token.size()-1] );
14             if ( iter == tokenSet.end() ) {
15                 num.push( atoi( token.c_str() ) );
16             } else {
17                int right = num.top();
18                num.pop();
19                int left = num.top();
20                num.pop();
21                switch ( *iter )
22                {
23                    case ADDITION :
24                        num.push(left + right);
25                        break;
26                    case SUBSTRACTION:
27                        num.push(left - right);
28                        break;
29                    case MULTIPLICATION:
30                        num.push(left * right);
31                        break;
32                    case DIVISION:
33                        num.push(left / right);
34                        break;
35                    default:
36                        break;
37                }
38             }
39         }
40         int ret = num.top();
41         num.pop();
42         return ret;
43     }
44 };
时间: 2024-10-13 02:28:56

LeetcodeOJ: Evaluate Reverse Polish Notation Stack的相关文章

[LeetCode] Evaluate Reverse Polish Notation stack 栈

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]Evaluate Reverse Polish Notation

题目:Evaluate Reverse Polish Notation 给出一个加减乘除的逆波兰式,求出它的结果: 什么是逆波兰式? 简单来说,逆波兰式就是表达式的后缀表示形式: 例如下面两个式子: ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 ["4", "13", "5", &quo

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", "+",

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: Reverse Words in a String:Evaluate Reverse Polish Notation

LeetCode: Reverse Words in a String: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&q

#Leet Code# Evaluate Reverse Polish Notation

描述:计算逆波兰表达法的结果 Sample: ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6 使用stack实现: 1 def is_op

【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]Evaluate Reverse Polish Notation @ Python

原题地址:https://oj.leetcode.com/problems/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

Evaluate Reverse Polish Notation (Python)

[问题] 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", "*"