LeetCode: Evaluate Reverse Polish Notation [150]

【题目】

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

【题意】

计算用逆波兰式表示的表达式的值

【思路】

逆波兰式其实是二叉树的遍历

用栈求解即可,每次遇到运算符是计算栈顶的两个元素

注意:

字符串转整数时,考虑负数的情况

【代码】

class Solution {
public:

    int str2int(string token){
        int num=0;
        int start=0;
        int isNev = 1;
        //判断符号
        if(token[0]=='-'){isNev=-1; start++;}
        else if(token[0]=='+')start++;
        for(int i=start; i<token.length(); i++){
            num=10*num+(token[i]-'0');
        }
        return num*isNev;
    }

    bool isOp(string token){
        if(token=="/" || token=="+" || token=="-" || token=="*")
            return true;
    }

    int evalRPN(vector<string> &tokens) {
        stack<int> st;
        for(int i=0; i<tokens.size(); i++){
            if(isOp(tokens[i])){
                //如果是运算符,则计算栈顶元素,然后将结果入栈
                int val1 = st.top(); st.pop();
                int val2 = st.top(); st.pop();
                int res = 0;
                if(tokens[i]=="+")  res = val2 + val1;
                else if(tokens[i]=="-")  res = val2 - val1;
                else if(tokens[i]=="*")  res = val2 * val1;
                else if(tokens[i]=="/")  res = val2 / val1;
                //计算结果入栈
                st.push(res);
            }
            else{
                //数字入栈
                st.push(str2int(tokens[i]));
            }
        }
        return st.top();
    }
};

LeetCode: Evaluate Reverse Polish Notation [150]

时间: 2024-10-13 11:35:41

LeetCode: Evaluate Reverse Polish Notation [150]的相关文章

[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

leetcode——Evaluate Reverse Polish Notation 求算式值(AC)

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", "*"] -&g

[LeetCode]Evaluate Reverse Polish Notation

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

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] 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 stack 栈

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", "*"] -&g

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

【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", "*"]