【Leetcode-medium-150】Evaluate Reverse Polish Notation

来源:互联网 发布:考研网络培训班 编辑:程序博客网 时间:2024/05/29 05:55

【Leetcode-medium-150】Evaluate Reverse Polish Notation

题目

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

思路

程序

import java.util.*;class Solution {    public int evalRPN(String[] tokens) {        if (tokens == null || tokens.length == 0)             throw new RuntimeException("illegal argument exception.");        LinkedList<Integer> stack = new LinkedList<>();        HashSet<String> set = new HashSet<>();        set.add("+");        set.add("-");        set.add("*");        set.add("/");        for (String token : tokens){            if (set.contains(token)){                int num2 = Integer.valueOf(stack.pop());                int num1 = Integer.valueOf(stack.pop());                switch(token){                    case "+" :                         stack.push(num1 + num2);                         break;                    case "-" :                        stack.push(num1 - num2);                        break;                    case "*" :                         stack.push(num1 * num2);                         break;                    case "/" :                        stack.push(num1 / num2);                        break;                }            }else{                stack.push(Integer.valueOf(token));            }        }        return stack.peek();    }}
阅读全文
1 0
原创粉丝点击