Evaluate Reverse Polish Notation (5)

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
标签:stack;这个问题是个后缀表达式问题,用stack堆栈来解决最简单有效。遇到数就将其压入堆栈,遇到运算符号就从栈顶取出两个数字参与运算。但是,要注意一个问题,每次弹出的两个数,其正确的运算顺序应该是后弹出的数在前,先弹出的数在后。举例说明:0 3 /弹出的时候是3先弹出,0次之,本来是0/3,如果没有按照正确的顺序,就变成了3/0,程序就会因为出现严重错误而崩溃了!感觉这个题目很简单,但是因为上述的问题搞了很久才弄好,在编程之前,一定要多思考再动手!
贴出代码:
 1 int istoken(string s){
 2     if(s.compare("+")==0)
 3         return 1;
 4     if(s.compare("-")==0)
 5         return 2;
 6     if(s.compare("*")==0)
 7         return 3;
 8     if(s.compare("/")==0)
 9         return 4;
10     return -1;
11 }//不同的返回值对应不同的情况
12 int transform(string s){
13     return atoi(s.c_str());//将string转换为int
14 }
15 class Solution {
16 public:
17     int evalRPN(vector<string> &tokens) {
18         int len=tokens.size();
19         int i;
20         stack<int>end;
21         for(i=0;i<len;i++){
22             if(istoken(tokens[i])==-1){//将数压入堆栈
23                 end.push(transform(tokens[i]));
24             }
25             else{
26                 int n1=end.top();
27                 end.pop();
28                 int n2=end.top();
29                 end.pop();//这里n1,n2交换了顺序
30                 switch(istoken(tokens[i])){
31                 case 1:{n1+=n2;break;}
32                 case 2:{n1-=n2;break;}
33                 case 3:{n1*=n2;break;}
34                 case 4:{n1/=n2;break;}
35                 }
36                 end.push(n1);
37             }
38
39         }
40         return end.top();
41     }
42 };
时间: 2024-10-05 10:07:44

Evaluate Reverse Polish Notation (5)的相关文章

[LeetCode]Evaluate Reverse Polish Notation

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

[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: 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)

逆波兰式的求解,建立一个类栈容器,遍历给定的逆波兰表达式,遇到数字就push, 遇到操作符就进行出栈,连续出两次,因为给定的四则运算符都是双目的,这里注意下这两个操作数的先后顺序,因为对于加法和乘法没关系,但是对于减法和除法是有先后关系的.然后进行相应的运算,将结果push进栈中. 这里附带说明下python中进行除法运算与c,java系列中的除法的不同,就是向下取整的问题.这种不同表现在两个操作数符号不同时的情况. 在c 中 3 / -5 = 0,但是在python中, 结果却为 - 1.这种

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