波兰式解析Evaluate Reverse Polish Notation

本文介绍了一种用于计算逆波兰表达式的算法,通过使用栈来处理包含加、减、乘、除的操作符和整数表达式。提供了两种Java实现版本,一种能够打印解析树并展示计算过程,另一种为更简洁快速的版本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文介绍波兰式解析算法。

Leetcode 原题如下

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

中文翻译如下,有效的操作符包括: +, -, *,/。 每个操作数是一个整数或者是另个一表达式。
例子如上图。

Java实现如下,包括两个版本。
版本一,包括打印解析树,及"((2+1)*3)->".
import java.util.Stack;

public class Solution {
	public static void main(String[] args){
		String[] input1 = {"2", "1", "+", "3", "*"};
		String[] input2 = {"4", "13", "5", "/", "+"};
		Solution s = new Solution();
		s.evalRPN(input1);
		s.evalRPN(input2);
	}
    class Node{
        int val;
        String sVal;
        Node(int val, String sval){
            this.val = val;
            this.sVal = sval;
        }
    }
    private boolean isNumber(String str){
        return str.matches("-?\\d+");
    }
    
    private boolean isOperator(String str){
        return str.matches("[/+*-]");
    }
    
    private int compute(int left, int right, char operator){
        switch(operator){
            case '/': return left / right;
            case '*': return left * right;
            case '-': return left - right;
            case '+': return left + right;
            //case default return 0;//need to handle this one
        }
		return 0;
    }
    public int evalRPN(String[] tokens) {
        Stack<Node> stack = new Stack();
        for(int i = 0; i < tokens.length; i++){
            String token = tokens[i];

            if(isNumber(token)) {
                int val = Integer.parseInt(token);
                stack.push(new Node(val, token));
            }else if(isOperator(token)){
                Node right = stack.pop();
                Node left = stack.pop();
                int tmp = compute(left.val, right.val, token.charAt(0));
                String stmp = "("+left.sVal + token + right.sVal+")";
                Node rst = new Node(tmp, stmp);
                stack.push(rst);
            }
        }
        int rval = 0;
        if(stack.size() == 1){
            Node node = stack.pop();
            System.out.println(node.sVal + "->" + node.val);
            rval = node.val;
        }
        return rval;
    }
}

输出结果如下:

((2+1)*3)->9

(4+(13/5))->6


版本二,简化版本,速度更快
public class Solution {
    private boolean isNumber(String str){
        return str.matches("-?\\d+");
    }
    
    private boolean isOperator(String str){
        return str.matches("[/+*-]");
    }
    
    private int compute(int left, int right, char operator){
        switch(operator){
            case '/': return left / right;
            case '*': return left * right;
            case '-': return left - right;
            case '+': return left + right;
            //case default return 0;//need to handle this one
        }
		return 0;
    }
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack();
        for(int i = 0; i < tokens.length; i++){
            String token = tokens[i];
            if(isNumber(token)) {
                int val = Integer.parseInt(token);
                stack.push(val);
            }else if(isOperator(token)){
                int right = stack.pop();
                int left = stack.pop();
                int tmp = compute(left,right, token.charAt(0));
                stack.push(tmp);
            }
        }
        int rval = 0;
        if(stack.size() == 1){
            rval = stack.pop();
        }
        return rval;
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值