资讯专栏INFORMATION COLUMN

[LeetCode] 150. Evaluate Reverse Polish Notation

KoreyLee / 2067人阅读

Problem

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

Solution
class Solution {
    public int evalRPN(String[] tokens) {
        Deque stack = new ArrayDeque<>();
        for (String token: tokens) {
            if (token.equals("+")) {
                stack.push(stack.pop()+stack.pop());
            } else if (token.equals("-")) {
                stack.push(-stack.pop()+stack.pop());
            } else if (token.equals("*")) {
                stack.push(stack.pop()*stack.pop());
            } else if (token.equals("/")) {
                int divisor = stack.pop();
                int dividend = stack.pop();
                stack.push(dividend/divisor);
            } else {
                stack.push(Integer.parseInt(token));
            }
        }
        return stack.pop();
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/72674.html

相关文章

  • leetcode150. Evaluate Reverse Polish Notation

    摘要:我们一般看到的数学表达式就是中缀表达式,也就是将符号放在两个数字之间。后缀表达式也就是将运算符放在相应数字的后面。后缀表达式相当于树中的后序遍历。通过获得对应位置的操作符。如果对应的还是操作符,则继续递归往前计算。 题目要求 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid...

    bitkylin 评论0 收藏0
  • 150. Evaluate Reverse Polish Notation

    摘要:题目链接来做,保存数字,碰到符号的时候就弹出两个数字计算算完后再放入,最后里面的就是结果。 150. Evaluate Reverse Polish Notation 题目链接:https://leetcode.com/problems... stack来做,保存数字,碰到符号的时候就弹出两个数字计算算完后再放入stack,最后stack里面的就是结果。 public class So...

    yanbingyun1990 评论0 收藏0
  • LeetCode 150:逆波兰表达式求值 Evaluate Reverse Polish Nota

    摘要:题目根据逆波兰表示法,求表达式的值。给定逆波兰表达式总是有效的。逆波兰表达式又叫做后缀表达式。解题思路可以看出逆波兰表达式中的每一个运算符属于该运算符前的两个数字间的运算。如如波兰表达式则加号前两个数字为。 题目: 根据逆波兰表示法,求表达式的值。 有效的运算符包括 +, -, *, / 。每个运算对象可以是整数,也可以是另一个逆波兰表达式。 Evaluate the value of...

    Turbo 评论0 收藏0
  • LeetCode 之 JavaScript 解答第150题 —— 逆波兰表达式求值

    摘要:小鹿题目根据逆波兰表示法,求表达式的值。给定逆波兰表达式总是有效的。算法思路仔细观察上述的逆波兰表达式,可以发现一个规律就是每遇到一个操作符,就将操作符前的两个操作数进行运算,将结果保存到原位置。 Time:2019/4/14Title: Evaluate Reverse Polish NotationDifficulty: MediumAuthor:小鹿 题目:Evaluate ...

    104828720 评论0 收藏0
  • [Leetcode] Evaluate Reverse Polish Notation 计算逆波兰表

    摘要:栈法复杂度时间空间思路逆波兰表达式的计算十分方便,对于运算符,其运算的两个数就是这个运算符前面的两个数。注意对于减法,先弹出的是减号后面的数。 Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operato...

    ephererid 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<