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. 2016HUAS_ACM暑假集训2L - Points on Cycle(圆上的点)

    一个简单的几何题,自己在纸上列出方程解出结果的表达式,再用程序表达出来就行了. 不过老司机(老司机的woodcoding)说用旋转向量法比较简单,有时间要去看一看. 大致题意:一个圆心在原点的圆,半径 ...

  2. CentOS 关闭防火墙和selinux

    1)关闭防火墙(每个节点) [Bash shell] 1 2 service iptables stop chkconfig iptables off 2)关闭selinux(重启生效) [Bash ...

  3. 解决IIS7该问.svc文件的错误问题

    解决IIS7.5中部署WCF时,访问.svc文件的404错误问题如果你直接在IIS 7中配置WCF,访问.svc文件时会出现404错误.解决方法,以管理员身份进入命令行模式,运行:"%win ...

  4. idea maven mvn archetype:generate 速度缓慢问题(转)

    From:https://my.oschina.net/u/225373/blog/468035   maven 骨架生成项目速度慢的令人发指,都在Generating project in Batc ...

  5. python编码-1

    help帮助系统,一个好的方法是直接看自带的帮助,尽量不用baidu help()是进入交互式帮助界面 quit是退出交互式帮助界面 [root@kvm1 python]# python Python ...

  6. Linux命令全集

    一.Ubuntu10.4 启动纯文件界面 打开 /etc/default/grub 文件, 注释掉  GRUB_CMDLINE_LINUX_DEFAULT="quiet splash&quo ...

  7. ios device model 详细内容

    参考 这里:https://theiphonewiki.com/wiki/Models http://en.wikipedia.org/wiki/List_of_iOS_devices http:// ...

  8. FM/PCM与FM/PPM的区别

    FM/PCM的优点:     1 高可靠性和高抗干扰性.大家知道,一般PPM遥控设备都要求在操作时先开发射机后开接收机,先关接收机后关发射机.其原因是在没有发射信号时,接受机会因自身内部的噪音或外界的 ...

  9. Intellij Idea系列之Tomcat环境的搭建(三)

    Intellij Idea系列之Tomcat环境的搭建(三) 一. 编写背景 Intellij Idea在刚上手的时候很多人吐槽,"god, 这么难用的IDE有谁用呀?",的确,I ...

  10. 【javascript基础】5、创建对象

    前言 今天从家里回到了学校,在家呆了十天,胖了几斤的重量,又折腾回学校了,春节回家真是艰辛的路途.随便扯扯我的往返行程:为了省钱我没有选择直飞到长春往返都是到北京转的,这样我和女朋友可以节省4000块 ...