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

【151-Evaluate Reverse Polish Notation(计算逆波兰式)】


【LeetCode-面试算法经典-Java实现】【所有题目目录索引】

原题

  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.Stack;

public class Solution {

    public int evalRPN(String[] tokens) {
        // 参数校验
        if (tokens == null || tokens.length < 1) {
            throw new IllegalArgumentException();
        }

        int op1;
        int op2;
        // 操作数栈
        Stack<Integer> stack = new Stack<>();

        for (String token: tokens) {
            // 说明是运算符,要取栈顶两个元素进行运算
            if ("+".equals(token) || "-".equals(token) || "*".equals(token) || "/".equals(token)) {
                // 取栈顶元素
                op2 = stack.pop();
                op1 = stack.pop();

                // 进行运算
                switch (token.charAt(0)) {
                    case ‘+‘:
                        op1 += op2;
                        break;
                    case ‘-‘:
                        op1 -= op2;
                        break;
                    case ‘*‘:
                        op1 *= op2;
                        break;
                    case ‘/‘:
                        op1 /= op2;
                        break;
                }
                // 结果入栈
                stack.push(op1);
            }
            // 说明是操作数,入栈
            else {
                stack.push(Integer.parseInt(token));
            }
        }

        return stack.pop();
    }
}

评测结果

  点击图片,鼠标不释放,拖动一段位置,释放后在新的窗口中查看完整图片。

特别说明

欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47801765

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-07 18:20:26

【LeetCode-面试算法经典-Java实现】【151-Evaluate Reverse Polish Notation(计算逆波兰式)】的相关文章

[Leetcode] 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", "*"] -> (

LeetCode OJ——练习笔记(1)Evaluate Reverse Polish Notation

院招终于开始了,然后期待与兴奋过后却是面临着笔试一次又一次的失败,然后开始留意到LeetCode. 也想自己去体验一下诸多大牛通向无限coding路上都攻克过的一关. 话不多说,贴出原题: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expres

【LeetCode刷题Java版】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", "*"] -&g

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", "*"] -&g

Evaluate Reverse Polish Notation leetcode java

题目: 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", "*"]

【leetcode】:Evaluate Reverse Polish Notation (python)

逆波兰式的求解,建立一个类栈容器,遍历给定的逆波兰表达式,遇到数字就push, 遇到操作符就进行出栈,连续出两次,因为给定的四则运算符都是双目的,这里注意下这两个操作数的先后顺序,因为对于加法和乘法没关系,但是对于减法和除法是有先后关系的.然后进行相应的运算,将结果push进栈中. 这里附带说明下python中进行除法运算与c,java系列中的除法的不同,就是向下取整的问题.这种不同表现在两个操作数符号不同时的情况. 在c 中 3 / -5 = 0,但是在python中, 结果却为 - 1.这种

[leetcode]Evaluate Reverse Polish Notation @ Python

原题地址:https://oj.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. Some examples

[LeetCode]Evaluate Reverse Polish Notation

题目:Evaluate Reverse Polish Notation 给出一个加减乘除的逆波兰式,求出它的结果: 什么是逆波兰式? 简单来说,逆波兰式就是表达式的后缀表示形式: 例如下面两个式子: ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 ["4", "13", "5", &quo

[LeetCode][JavaScript]Evaluate Reverse Polish Notation

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", "+",