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:

import java.util.*;
public class Solution {
    public int evalRPN(String[] tokens) {
       
        //int temp = 0;
       Stack<Integer> stack = new Stack<Integer>();
        for( int i = 0; i < tokens.length; i++){
            if(tokens[i].equals("+") || tokens[i].equals("-") || tokens[i].equals("*") || tokens[i].equals("/")){
                int a = stack.pop();
                int b = stack.pop();
                //temp =  calculateMethod(tokens[i],a,b);
                stack.push(calculateMethod(tokens[i],b));
            }else{
                stack.push(Integer.parseInt(tokens[i]));
            }
        }
        return stack.pop();
    }

    public  int calculateMethod(String tokens,int a,int b)  {
        if(tokens.equals("+")){
            return b + a;
        }else if(tokens.equals("-")){
            return b - a;
        }else if(tokens.equals("*")){
            return b * a;
        }
        else{
            //int c= b/a;
           // System.out.println(c);
            return b/a;
        }
    }
}

相关文章

1.github代码实践源代码是lua脚本语言,下载th之后运行thmai...
此文为搬运帖,原帖地址https://www.cnblogs.com/zwywilliam/...
Rime输入法通过定义lua文件,可以实现获取当前时间日期的功能...
localfunctiongenerate_action(params)localscale_action=cc...
2022年1月11日13:57:45 官方:https://opm.openresty.org/官...
在Lua中的table(表),就像c#中的HashMap(哈希表),key和...