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

  • 题目大意:给定一个逆波兰表达式,求该表达式的值
  • 思路:由于逆波兰表达式本身不需要括号来限制哪个运算该先进行,因此可以直接利用栈来模拟计算:遇到操作数直接压栈,碰到操作符直接取栈顶的2个操作数进行计算(注意第一次取出来的是右操作数),然后再把计算结果压栈,如此循环下去。最后栈中剩下的唯一一个元素便是整个表达式的值
  • 实现:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class  Solution {
public :
     int  evalRPN(vector<string> &tokens) {
         
         int  result = 0;
         int  i;
         stack< int > opd;          //存储操作数
         int  size = tokens.size();
         for (i=0;i<size;i++)
         {
             if (tokens[i]== "*" )
             {
                 int  rOpd = opd.top();    //右操作数
                 opd.pop();
                 int  lOpd = opd.top();   //左操作数
                 opd.pop();
                 result = lOpd*rOpd;
                 opd.push(result);
             }
             else  if (tokens[i]== "/" )
             {
                 int  rOpd = opd.top();
                 opd.pop();
                 int  lOpd = opd.top();
                 opd.pop();
                 result = lOpd/rOpd;
                 opd.push(result);
             }
             else  if (tokens[i]== "+" )
             {
                 int  rOpd = opd.top();
                 opd.pop();
                 int  lOpd = opd.top();
                 opd.pop();
                 result = lOpd+rOpd;
                 opd.push(result);
             }
             else  if (tokens[i]== "-" )
             {
                 int  rOpd = opd.top();
                 opd.pop();
                 int  lOpd = opd.top();
                 opd.pop();
                 result = lOpd-rOpd;
                 opd.push(result);
             }
             else
             {
                 opd.push( atoi (tokens[i].c_str()));
             }
         }
         return  opd.top();
     }
};

本文转载自海 子博客园博客,原文链接:http://www.cnblogs.com/dolphin0520/p/3708587.html如需转载自行联系原作者

相关文章
|
3月前
|
数据处理 开发者 Python
【Python】已解决:ValueError: Length mismatch: Expected axis has 5 elements, new values have 4 elements
【Python】已解决:ValueError: Length mismatch: Expected axis has 5 elements, new values have 4 elements
108 9
|
3月前
|
SQL 数据库连接 数据库
【Python】已完美解决:executemany() takes exactly 2 positional arguments (3 given)
【Python】已完美解决:executemany() takes exactly 2 positional arguments (3 given)
40 6
|
机器学习/深度学习 索引 Python
【Python·问题解决】IndexError: too many indices for array: array is 2-dimensional, but 3 were indexed
【Python·问题解决】IndexError: too many indices for array: array is 2-dimensional, but 3 were indexed
【Python·问题解决】IndexError: too many indices for array: array is 2-dimensional, but 3 were indexed
LeetCode 150. Evaluate Reverse Polish Notation
根据逆波兰表示法,求表达式的值。 有效的运算符包括 +, -, *, / 。每个运算对象可以是整数,也可以是另一个逆波兰表达式。
42 0
LeetCode 150. Evaluate Reverse Polish Notation
Data Structures and Algorithms (English) - 6-5 Evaluate Postfix Expression(25 分)
Data Structures and Algorithms (English) - 6-5 Evaluate Postfix Expression(25 分)
114 0
成功解决gensim\matutils.py:737: FutureWarning: Conversion of the second argument of issubdtype from `int
成功解决gensim\matutils.py:737: FutureWarning: Conversion of the second argument of issubdtype from `int
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. 说明: 整数除法只保留整数部分。
1373 0
|
Python
lecture 1 isinstance()函数
isinstance()与 type()区别:  type()不会认为子类是一种父类类型,不考虑继承关系。  isinstance()会认为子类是一种父类类型,考虑继承关系。
1238 0