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. 化繁为简 如何向老婆解释MapReduce?(转载)

    化繁为简 如何向老婆解释MapReduce? 昨天,我在Xebia印度办公室发表了一个关于MapReduce的演说.演说进行得很顺利,听众们都能够理解MapReduce的概念(根据他们的反馈).我成功 ...

  2. knockout 学习实例7 foreach

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  3. 什么是 Web API

    http://www.cnblogs.com/developersupport/p/aspnet-webapi.html Web API 强势入门指南 Web API是一个比较宽泛的概念.这里我们提到 ...

  4. HackerRank "TBS Problem" ~ NPC

    It is marked as a NPC problem. However from the #1 code submission (https://www.hackerrank.com/Charl ...

  5. linux net command /uboot command

    1. uboot command 读取寄存器内容 md [内存地址][长度] 修改内存地址 mw [内存地址][长度] 2. linux络命令  -- netstat -a (all)显示所有选项,默 ...

  6. mac与php环境

    一.目录 apache目录:/etc/apache2/ mysql目录:/usr/local/mysql/ 站点目录:/Library/WebServer/Documents/ 二.mac系统给文件夹 ...

  7. firefox与IE对js和CSS的区别(转http://log-cd.javaeye.com/blog/548665)

     ? "700px" : document.body.clientWidth>1000 ? "1000px" : "auto");// ...

  8. 安装android studio

    sudo apt-get install lib32z1 lib32ncurses5 lib32bz2-1.0 lib32stdc++6 sudo apt-get install g++-multil ...

  9. Delphi完成的断点续传例子 转

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  10. READONLY、、cursor、、VARYING

    针对 Transact-SQL 过程的准则:所有 Transact-SQL 数据类型都可以用作参数.您可以使用用户定义的表类型创建表值参数.表值参数只能是 INPUT 参数,并且这些参数必须带有 RE ...