[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", "*"] -> ((2 + 1) * 3) -> 9
  ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6

Hide Tags

Stack


    题目是用 stack 维护一个公式的进出,判断方法还可以,开始忘记数可能为负,后面改进了。

#include <stack>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Solution {
public:
    int evalRPN(vector<string> &tokens) {
        int n = tokens.size();
        stack<int > tmp;
        for(int i=0;i<n;i++){
            if(tokens[i][0]>=‘0‘&&tokens[i][0]<=‘9‘){
                tmp.push( helpFun(tokens[i]) );
                continue;
            }
            if(tokens[i][0]==‘-‘&&tokens[i][1]!=‘\0‘){
                tmp.push( helpFun( tokens[i]));
                continue;
            }
            int rgt = tmp.top();
            tmp.pop();
            int lft = tmp.top();
            tmp.pop();
            switch (tokens[i][0]){
            case ‘+‘:
                tmp.push( lft + rgt );
                break;
            case ‘-‘:
                tmp.push(lft - rgt);
                break;
            case ‘*‘:
                tmp.push(lft * rgt);
                break;
            case ‘/‘:
                tmp.push(lft / rgt);
                break;
            }
        }
        return tmp.top();
    }

    int helpFun(string str)
    {
        int sum = 0,i = 0;
        if (str[0]==‘-‘)
            i = 1;
        for(;i<str.length();i++)
            sum = sum*10+str[i]-‘0‘;
        return str[0]==‘-‘?-sum:sum;
    }
};

int main()
{
    vector<string> tokens{"3","-4","+"};
    Solution sol;
    cout<<sol.evalRPN(tokens)<<endl;
//    for(int i=0;i<tokens.size();i++)
//        cout<<tokens[i]<<endl;
    return 0;
}
时间: 2024-07-31 14:21:15

[LeetCode] Evaluate Reverse Polish Notation stack 栈的相关文章

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

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

LeetcodeOJ: Evaluate Reverse Polish Notation Stack

1 #define ADDITION '+' 2 #define SUBSTRACTION '-' 3 #define MULTIPLICATION '*' 4 #define DIVISION '/' 5 6 7 class Solution { 8 public: 9 set<char> tokenSet{'+', '-', '*', '/'}; 10 stack<int> num; 11 int evalRPN(vector<string> &tokens

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