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

计算逆波兰表达式的值,这个感觉比较简单,就是利用一个栈,把数字都进栈,然后碰到一个符号就出栈两个数字,把结果进栈,直到表达式结束或栈空。
就是这么简陋的写法,不过至少能AC是不

class Solution {
public:
    int evalRPN(vector& tokens) {
        int n = tokens.size();
        stack s;
        for (int i=0; i

有点长,写的简洁一点…

class Solution {
public:
    int evalRPN(vector& tokens) {
        int n = tokens.size();
        stack s;
        for (int i=0; i

不过写简洁了,时间变长了一些,不过其实复杂度是一样的是不。

你可能感兴趣的:(Evaluate Reverse Polish Notation)