Leetcode 之Evaluate Reverse Polish Notation(41)

很简单的一道题,定义一个栈保留操作数,遇操作符则弹出运算即可。

bool isOperator(string &op)
      {
          //注意用法
          return op.size() == 1 && string("+-*/").find(op) != string::npos;
      }
      int evalRPN(vector<string> &tokens)
      {
          stack<string> s;
          for (auto token : tokens)
          {
              if (!isOperator(token))
              {
                  //如果是操作数,则入栈
                  s.push(token);
              }
              else
              {
                  //如果是操作符,则弹出操作数进行运算
                  int y = stoi(s.top());
                  s.pop();
                  int x = stoi(s.top());
                  s.pop();
                  if (token == "+")x += y;
                  if (token == "-")x -= y;
                  if (token == "*")x *= y;
                  if (token == "/")x /= y;
                  s.push(to_string(x));
              }
          }
          return stoi(s.top());
      }

时间: 2024-10-12 15:09:58

Leetcode 之Evaluate Reverse Polish Notation(41)的相关文章

【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][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", "+",

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