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. git相关

    进入到想要用git管理的project目录下 1.git init 意即该目录会被git监视一切的变动 同时生成一个.git文件夹下面存放了管理该project的一切必要信息 2.git add &l ...

  2. 使用 dynamic 标记解析JSON字符串

    string jsonStr = "{\"data\": {\"ssoToken\": \"70abd3d8a6654ff189c482fc ...

  3. spring-实现配置文件读取

    spring 实现配置读取 Java 的配置读取方式一般是采用java.utils.Properties 或是apache的Configuration工具:然而 spring 框架内置了配置文件的读取 ...

  4. spring boot Swagger 集成

    1. pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://ww ...

  5. 【线性代数】 06 - Jordan标准型

    现在就来研究将空间分割为不变子空间的方法,最困难的是我们还不知道从哪里着手.你可能想到从循环子空间出发,一块一块地进行分割,但这个方案的存在性和唯一性都不能解决.不变子空间分割不仅要求每个子空间\(V ...

  6. Microsoft .NET Framework NGEN是神马东东?

    简单的说,如果你的程序是基于.net framework的托管代码的话,NGEN服务能让你的程序第二次打开的速度变快. 赶脚是非常pad化的一项服务. http://msdn.microsoft.co ...

  7. Openstack Neutron L2 Population

    Why do we need it, whatever it is? VM unicast, multicast and broadcast traffic flow is detailed in m ...

  8. Silverlight管理系统源码(用于开发ERP、OA、CRM、HR、进销存、财务等系统之用)

    Silverlight大型管理系统源代码(支持创建ERP.OA.CRM.HR.进销存.财务等系统之用) 可用于开发以下系统 SilverlightERP SilverlightCRM Silverli ...

  9. 认识angualrJS的resource服务

    这段时间公司有个项目要用到angularJS,于是就在网上开始各种找学习资料. 一开始下了一本<angularJS权威教程>,看了10章,实在看不下去了,只能说这本书对于才接触javasc ...

  10. Hololens开发笔记之Gesture手势识别(手势检测反馈)

    本文实现当使用者手出现在Hololens视野范围内时,跟踪手并给出反馈的效果. 1.在Manager上添加HandsManager脚本组件,用于追踪识别手 HandsManager.cs如下(直接使用 ...