Evaluate the value of an arithmetic expression in Reverse Po
发布时间:2020-12-14 21:43:07 所属栏目:大数据 来源:网络整理
导读:import java.util.* ; public class Solution { public int evalRPN(String[] tokens) { // int temp = 0; StackInteger stack = new StackInteger (); for ( int i = 0; i tokens.length; i++ ){ if (tokens[i].equals("+") || tokens[i].equals("-") || to
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; } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |