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

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

  • 解题思路
    用一个栈来存储数字,从第一个数开始遍历vector<string> tokens,直到最后一个元素。如果遍历到的元素为操作符,则从栈中弹出两个操作数,将其与操作符进行运算,并将运算结果入栈。遍历完成后,栈中的元素即为所求。
  • 实现代码
/*****************************************************************
    *  @Author   : 楚兴
    *  @Date     : 2015/2/9 15:07
    *  @Status   : Accepted
    *  @Runtime  : 20 ms
******************************************************************/
class Solution {
public:
    int evalRPN(vector<string> &tokens) {
        stack<int> nums;
        int i = 0;
        int n1, n2;
        while (i < tokens.size())
        {
            if (isToken(tokens[i]))
            {
                n1 = nums.top();
                nums.pop();
                n2 = nums.top();
                nums.pop();
                //特别注意:第二次弹出数字n2为第一个操作数
                nums.push(calc(n2,n1,tokens[i]));
                i++;
            }
            else
            {
                nums.push(strToInt(tokens[i]));
                i++;
            }
        }

        return nums.top();
    }

    int calc(int n1, int n2, string op)
    {
        char ch = op[0];
        switch(ch)
        {
        case '+':
            return n1 + n2;
            break;
        case '-':
            return n1 - n2;
            break;
        case '*':
            return n1 * n2;
            break;
        case '/':
            return n1 / n2;
            break;
        }
    }

    bool isToken(string str)
    {
        if (str.size() == 1 && (str[0] < '0' || str[0] > '9'))
        {
            return true;
        }
        return false;
    }

    int strToInt(string str)
    {
        int sum = 0;
        bool flag = false;
        int i = 0;
        while (str[i] == ' ')
        {
            i++;
        }

        if (str[i] == '-')
        {
            flag = true;
            i++;
        }
        else if (str[i] == '+')
        {
            i++;
        }

        while (i < str.size())
        {
            sum = sum * 10 + str[i++] - '0';
        }

        if (flag)
        {
            return -sum;
        }
        return sum;
    }
};

例如,表达式“8 – ((1 + 2) * 2)”用RPN表示为:8 1 2 + 2 * –

Input Operation Stack Notes
8 压入操作数 8
1 压入操作数 8 1
2 压入操作数 8 1 2
+ 相加 8 3 弹出1、2,压入结果3
2 压入操作数 8 3 2
* 相乘 8 6 弹出3、2,压入结果6
- 相减 2 弹出8、6,压入结果2

算法结束后,栈中只有一个元素,它就是RPN表达式的结果,本例子中结果为2。

目录
相关文章
|
机器学习/深度学习 NoSQL 算法
LeetCode 344. 反转字符串 Reverse String
LeetCode 344. 反转字符串 Reverse String
LeetCode 206. 反转链表 Reverse Linked List
LeetCode 206. 反转链表 Reverse Linked List
|
索引
LeetCode 345. Reverse Vowels of a String
编写一个函数,以字符串作为输入,反转该字符串中的元音字母。
54 0
LeetCode 345. Reverse Vowels of a String
|
机器学习/深度学习 NoSQL
LeetCode 344. Reverse String
编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组 char[] 的形式给出。 不要给另外的数组分配额外的空间,你必须原地修改输入数组、使用 O(1) 的额外空间解决这一问题。
72 0
LeetCode 344. Reverse String
LeetCode 190. Reverse Bits
颠倒给定的 32 位无符号整数的二进制位。
66 0
LeetCode 190. Reverse Bits
LeetCode 150. Evaluate Reverse Polish Notation
根据逆波兰表示法,求表达式的值。 有效的运算符包括 +, -, *, / 。每个运算对象可以是整数,也可以是另一个逆波兰表达式。
29 0
LeetCode 150. Evaluate Reverse Polish Notation
LeetCode 92. Reverse Linked List II
给定一个链表,反转指定的子序列.
60 0
LeetCode 92. Reverse Linked List II
LeetCode之Reverse String II
LeetCode之Reverse String II
86 0
LeetCode之Reverse String
LeetCode之Reverse String
71 0
LeetCode之Reverse Integer
LeetCode之Reverse Integer
80 0