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

Subscribe to see which companies asked this question

解答:

这道题只要注意有负数就可以了。

int evalRPN(char** tokens, int tokensSize) {
    int stack[tokensSize];
    , sum, i, j;

    ; i < tokensSize; i++){
        '){
            sum = ;
            ; tokens[i][j] != ; j++){
                sum = sum *  + tokens[i][j] - ';
            }
            stack[++top] = sum;
        }
        else{
            if('+' == *(tokens[i])){
                stack[top - ] = stack[top - ] + stack[top];
            }
            else if('-' == *(tokens[i])){
                 == tokens[i][])
                    stack[top - ] = stack[top - ] - stack[top];
                else{
                    sum = ;
                    ; tokens[i][j] != ; j++){
                        sum = sum *  + tokens[i][j] - ';
                    }
                    stack[++top] = -sum;
                    continue;
                }
            }
            else if('*' == *(tokens[i])){
                stack[top - ] = stack[top - ] * stack[top];
            }
            else if('/' == *(tokens[i])){
                stack[top - ] = stack[top - ] / stack[top];
            }
            top--;
        }
    }
    return stack[top];
}

LeetCode OJ 150. Evaluate Reverse Polish Notation的更多相关文章

  1. 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)

    [LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...

  2. 【LeetCode】150. Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

  3. 【刷题-LeetCode】150 Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

  4. LeetCode OJ:Evaluate Reverse Polish Notation(逆波兰表示法的计算器)

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  5. 【LeetCode OJ】Evaluate Reverse Polish Notation

    Problem link: http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ According to the wik ...

  6. 150. Evaluate Reverse Polish Notation - LeetCode

    Question 150. Evaluate Reverse Polish Notation Solution 2 1 + 3 * 是((2+1)*3)的后缀(postfix)或逆波兰(reverse ...

  7. 【LeetCode练习题】Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

  8. [LeetCode] 150. Evaluate Reverse Polish Notation 计算逆波兰表达式

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  9. Java for LeetCode 150 Evaluate Reverse Polish Notation

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

随机推荐

  1. 《统计推断(Statistical Inference)》读书笔记——第5章 随机样本的性质

    有了前四章知识的铺垫,第五章进入了统计研究的正题——样本的研究.样本可以说是统计学研究中最基本的对象,样本的数学性质也是最重要的研究课题,统计学的一大任务就是从一大堆样本中提取出有价值的知识,正如对原 ...

  2. 黑马程序员——JAVA基础之File类,递归,打印流,合并切割流

    ------- android培训.java培训.期待与您交流! ---------- File类 用来将文件或者文件夹封装成对象 方便对文件与文件夹的属性信息进行操作. File对象可以作为参数传递 ...

  3. java Scanner

    public static void main(String[] args) throws IOException { System.out.print("Enter a number:&q ...

  4. JDBC连接数据库操作

    JDBC连接数据库 •创建一个以JDBC连接数据库的程序,包含7个步骤: 1.加载JDBC驱动程序: 在连接数据库之前,首先要加载想要连接的数据库的驱动到JVM(Java虚拟机), 这通过java.l ...

  5. 使用APC缓存PHP opcode

    1.  关于版本 我的php版本是5.3.10  最开始使用 APC-3.1.8  编译成功,但是 php -m 得不到扩展 , 换成 APC-3.1.9  后解决. 2.  关于配置 apc.cac ...

  6. 【转】vc++MCF/C++/C中怎样让应用程序获得或以管理员权限运行 ,ShellExecuteEX编程 --- 获取管理员权限

    ShellExecuteEX编程 --- 获取管理员权限:http://blog.csdn.net/jhui163/article/details/5873027 怎样让你的应用程序获得管理员权限:就 ...

  7. c#定义全局条件编译符号

    在"工程"上单机右键,"属性"--->"生成"--->"条件编译符号"后边的输入框中,输入自定义的条件编译变 ...

  8. Java事务处理全解析(八)——分布式事务入门例子(Spring+JTA+Atomikos+Hibernate+JMS)

    在本系列先前的文章中,我们主要讲解了JDBC对本地事务的处理,本篇文章将讲到一个分布式事务的例子. 请通过以下方式下载github源代码: git clone https://github.com/d ...

  9. [Tex学习笔记]开方

    $$\sqrt[n]{\frac{a}{b}}$$ $$\sqrt[\uproot{7}n]{\frac{a}{b}}$$

  10. 【转】Cordova文件传输插件fileTransfer

    任务要求: 访问手机的目录,选择一个文件,并使用该插件将指定文件传输到远程主机的某个指定目录中. HTML <!DOCTYPE html> <!-- Licensed to the ...