LeetCode 150. Evaluate Reverse Polish Notation(java)

原创 2018年02月04日 06:11:03

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
这道题在解法思路上没有什么问题,用stack预存前两个数,每次遇到一个operator就从stack中pop出两个数计算,再塞回去,但是有很多比较tricky的点,比如:1. stack有可能为空,无法pop出数据,我们的解决方法就是抛出IllegalArgumentException。2. 从stack中pop出数字进行运算的时候要把第二个pop出的数据当做被运算数,同时要注意除法的时候除数不为0。3. 栈中的数字可能为负数,因此我们在自己写isDigit函数的时候要记住第一个字符可以是‘-’,但是不能只有一个‘-’字符。
public int evalRPN(String[] tokens) {
        //special case
        if (tokens == null) return -1;
        if (tokens.length == 1) {
            return Integer.parseInt(tokens[0]);
        }
        if (tokens.length < 3) return -1;
        //general case
        Stack<Integer> stack = new Stack<>();
        for (String s : tokens) {
            if (s == null || s == "") return -1;
            if (isDigit(s)) {
                stack.push(Integer.parseInt(s));
            } else {
                try {
                    int num1 = stack.pop();
                    int num2 = stack.pop();
                    int num = 0;
                    if (s.equals("+")) {
                        num = num1 + num2;
                    } else if (s.equals("-")) {
                        num = num2 - num1;
                    } else if (s.equals("*")) {
                        num = num1 * num2;
                    } else if (s.equals("/")) {
                        if (num1 == 0) return -1;
                        num = num2 / num1;
                    }
                    stack.push(num);
                } catch (IllegalArgumentException e) {
                    return -1;
                }
            }
        }
        if (stack.isEmpty()) return -1;
        else {
            int res = stack.pop();
            return res;
        } 
    }
    public boolean isDigit(String s) {
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(0) == '-') {
                if (s.length() == 1) return false;
                else continue;
            }
            char c = s.charAt(i);
            if (!Character.isDigit(c)) {
                return false;
            }
        }
        return true;
    }

Java学习指南系列(Java快速入门)

-
  • 1970年01月01日 08:00

【LeetCode】Evaluate Reverse Polish Notation 解题报告

【题意】 逆波兰表达式,又叫后缀表达式。 例如: ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 ["4", "13", "5", "/", "+"]...
  • ljiabin
  • ljiabin
  • 2014-08-28 20:57:36
  • 1802

LeetCode 150 — Evaluate Reverse Polish Notation(C++ Java Python)

题目:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ Evaluate the value of an arithm...
  • dragon_dream
  • dragon_dream
  • 2014-03-01 15:34:39
  • 2532

[leetcode-150]Evaluate Reverse Polish Notation(c++)

问题描述: Evaluate the value of an arithmetic expression in Reverse Polish Notation.Valid operators are...
  • zdavb
  • zdavb
  • 2015-08-20 15:25:45
  • 546

LeetCode–Evaluate Reverse Polish Notation

LeetCode–Evaluate Reverse Polish Notation (LeetCode - 评价逆波兰表示法,即后缀表达式) By X Wang (LeetCode: 一个在线编...
  • zgljl2012
  • zgljl2012
  • 2014-05-05 00:59:16
  • 1093

Evaluate Reverse Polish Notation -- LeetCode

原题链接: http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/  这道题是逆波兰式的求解,不了解逆波兰式的朋友可以参考一...
  • linhuanmars
  • linhuanmars
  • 2014-03-13 21:56:02
  • 5442

LeetCode150—Evaluate Reverse Polish Notation

LeetCode150—Evaluate Reverse Polish Notation
  • zhangxiao93
  • zhangxiao93
  • 2016-10-19 23:14:25
  • 375

Java Evaluate Reverse Polish Notation(逆波兰表达式)

表达式:   ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9   ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)...
  • soszou
  • soszou
  • 2014-07-05 16:06:45
  • 2796

【LeetCode刷题Java版】Evaluate Reverse Polish Notation(计算逆波兰表达式)

Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -...
  • bruce_6
  • bruce_6
  • 2014-10-13 18:02:40
  • 2464

【LeetCode-面试算法经典-Java实现】【150-Evaluate Reverse Polish Notation(计算逆波兰式)】

【151-Evaluate Reverse Polish Notation(计算逆波兰式)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Evaluate the val...
  • DERRANTCM
  • DERRANTCM
  • 2015-08-20 06:37:59
  • 2829
收藏助手
不良信息举报
您举报文章:LeetCode 150. Evaluate Reverse Polish Notation(java)
举报原因:
原因补充:

(最多只允许输入30个字)