150. 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
 1 class Solution:
 2     def evalRPN(self, tokens):
 3         """
 4         :type tokens: List[str]
 5         :rtype: int
 6         """
 7         stack = []
 8         for i in tokens:
 9             if i not in [‘+‘,‘-‘,‘*‘,‘/‘]:
10                 stack.append(i)
11             else:
12                 v2 = int(stack.pop())
13                 v1 = int(stack.pop())
14                 if i==‘+‘:
15                     stack.append(v1+v2)
16                 if i==‘-‘:
17                     stack.append(v1-v2)
18                 if i==‘*‘:
19                     stack.append(v1*v2)
20                 if i==‘/‘:
21                     stack.append(v1/v2)
22         return int(stack[0])
23         

原文地址:https://www.cnblogs.com/zle1992/p/8469734.html

时间: 2024-11-03 22:39:37

150. Evaluate Reverse Polish Notation(逆波兰表达式)的相关文章

Java Evaluate Reverse Polish Notation(逆波兰表达式)

表达式:: ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9   ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6 题目大意:给定一个逆波兰表达式,求该表达式的值 思路:由于逆波兰表达式本身

lintcode 中等题:Evaluate Reverse Polish notation逆波兰表达式求值

题目 逆波兰表达式求值 在逆波兰表达法中,其有效的运算符号包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰计数表达. 样例 ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 ["4", "13", "5", "/", "+"]

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

Java for LeetCode 150 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

[leedcode 150] 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

150. Evaluate Reverse Polish Notation QuestionEditorial Solution

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

力扣算法题—150. 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. Note: Division between two integers should truncate toward zero. The given RPN expression

leetcode[150] Evaluate Reverse Polish Notation

逆波兰表示法,在维基百科here 一不小心就看到了维基上有说用栈处理.然后就用栈处理了. 需要注意的是,操作数前后不要弄错,stoi可以调用.它应该是在stdlib.h的头文件里,不过我在codeblock上试了不行. class Solution { public: int evalRPN(vector<string> &tokens) { int size = tokens.size(); if (size == 0) return 0; stack<int> sta;