其他分享
首页 > 其他分享> > LeetCode 150. Evaluate Reverse Polish Notation

LeetCode 150. Evaluate Reverse Polish Notation

作者:互联网

分析

难度 中
来源 https://leetcode.com/problems/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.
Note:
• Division between two integers should truncate toward zero.
• The given RPN expression is always valid. That means the expression would always evaluate to a result and there won’t be any divide by zero operation.

Example 1:

Input: [“2”, “1”, “+”, “3”, “*”]
Output: 9
Explanation: ((2 + 1) * 3) = 9

Example 2:

Input: [“4”, “13”, “5”, “/”, “+”]
Output: 6
Explanation: (4 + (13 / 5)) = 6

Example 3:

Input: [“10”, “6”, “9”, “3”, “+”, “-11”, “", “/”, "”, “17”, “+”, “5”, “+”]
Output: 22
Explanation:
((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22

解答

Runtime: 6 ms, faster than 90.38% of Java online submissions for Evaluate Reverse Polish Notation.
Memory Usage: 38.4 MB, less than 28.30% of Java online submissions for Evaluate Reverse Polish Notation.

package LeetCode;

import java.util.HashSet;
import java.util.Stack;

public class L150_EvaluateReversePolishNotation {
    public int evalRPN(String[] tokens) {
        HashSet<String> operates=new HashSet<String>();
        operates.add("+");
        operates.add("-");
        operates.add("*");
        operates.add("/");
        Stack<Integer> stack=new Stack<Integer>();
        for(int i=0;i<tokens.length;i++)
        {
            if(!operates.contains(tokens[i]))//非符号
                stack.push(Integer.valueOf(tokens[i]));
            else
            {
                Integer num2=stack.pop();
                Integer num1=stack.pop();
                switch (tokens[i]){
                    case "+":
                        stack.push(num1+num2);
                        break;
                    case "-":
                        stack.push(num1-num2);
                        break;
                    case "*":
                        stack.push(num1*num2);
                        break;
                    case "/":
                        stack.push(num1/num2);
                        break;
                    /*default:
                        stack.push(Integer.valueOf(tokens[i]));
                        break;*/
                }
            }
        }
        return stack.peek();
    }
    public static void main(String[] args){
        L150_EvaluateReversePolishNotation l150=new L150_EvaluateReversePolishNotation();
        //String[] tokens={"2", "1", "+", "3", "*"};
        String[] tokens={"4", "13", "5", "/", "+"};
        System.out.println(l150.evalRPN(tokens));
    }
}

标签:150,Reverse,17,10,Evaluate,Notation,operates,add,Polish
来源: https://blog.csdn.net/leechengqian/article/details/88049334