1. 程式人生 > >[LeetCode] Evaluate Reverse Polish Notation 計算逆波蘭表示式

[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.

Note:

  • Division between two integers should truncate toward zero.
  • The given RPN expression is always valid. That means the expression would always evaluate to a result and there won't be any divide by zero operation.

Example 1:

Input: ["2", "1", "+", "3", "*"]
Output: 9
Explanation: ((2 + 1) * 3) = 9

Example 2:

Input: ["4", "13", "5", "/", "+"]
Output: 6
Explanation: (4 + (13 / 5)) = 6

Example 3:

Input: ["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"]
Output: 22
Explanation: 
  ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22

逆波蘭表示式就是把運算元放前面,把操作符後置的一種寫法,我們通過觀察可以發現,第一個出現的運算子,其前面必有兩個數字,當這個運算子和之前兩個數字完成運算後從原陣列中刪去,把得到一個新的數字插入到原來的位置,繼續做相同運算,直至整個陣列變為一個數字。於是按這種思路寫了程式碼如下,但是拿到OJ上測試,發現會有Time Limit Exceeded的錯誤,無奈只好上網搜答案,發現大家都是用棧做的。仔細想想,這道題果然應該是棧的完美應用啊,從前往後遍歷陣列,遇到數字則壓入棧中,遇到符號,則把棧頂的兩個數字拿出來運算,把結果再壓入棧中,直到遍歷完整個陣列,棧頂數字即為最終答案。程式碼如下:

解法一:

class Solution {
public:
    int evalRPN(vector<string>& tokens) {
        if (tokens.size() == 1) return stoi(tokens[0]);
        stack<int> st;
        for (int i = 0; i < tokens.size(); ++i) {
            if (tokens[i] != "+" && tokens[i] != "-" && tokens[i] != "*" && tokens[i] != "/") {
                st.push(stoi(tokens[i]));
            } else {
                int num1 = st.top(); st.pop();
                int num2 = st.top(); st.pop();
                if (tokens[i] == "+") st.push(num2 + num1);
                if (tokens[i] == "-") st.push(num2 - num1);
                if (tokens[i] == "*") st.push(num2 * num1);
                if (tokens[i] == "/") st.push(num2 / num1);
            }
        }
        return st.top();
    }
};

我們也可以用遞迴來做,由於一個有效的逆波蘭表示式的末尾必定是操作符,所以我們可以從末尾開始處理,如果遇到操作符,向前兩個位置呼叫遞迴函式,找出前面兩個數字,然後進行操作將結果返回,如果遇到的是數字直接返回即可,參見程式碼如下:

解法二:

class Solution {
public:
    int evalRPN(vector<string>& tokens) {
        int op = (int)tokens.size() - 1;
        return helper(tokens, op);
    }
    int helper(vector<string>& tokens, int& op) {
        string str = tokens[op];
        if (str != "+" && str != "-" && str != "*" && str != "/") return stoi(str);
        int num1 = helper(tokens, --op);
        int num2 = helper(tokens, --op);
        if (str == "+") return num2 + num1;
        if (str == "-") return num2 - num1;
        if (str == "*") return num2 * num1;
        return num2 / num1;
    }
};

類似題目:

參考資料: