[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;
    }
};

 

類似題目:

Basic Calculator

Expression Add Operators

 

參考資料:

https://leetcode.com/problemset/algorithms/

https://leetcode.com/problems/evaluate-reverse-polish-notation/discuss/47642/a-recursive-solution-in-cpp

https://leetcode.com/problems/evaluate-reverse-polish-notation/discuss/47544/Challenge-me-neat-C%2B%2B-solution-could-be-simpler

 

LeetCode All in One 題目講解匯總(持續更新中...)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM