package leetcode; import java.util.Stack; public class RPN {
public static int evalRPN(String[] tokens) {
Stack stack=new Stack();
for(int i=0;i<tokens.length;i++){
if(tokens[i]=="+"){
int a=Integer.parseInt((String)stack.pop());
int b=Integer.parseInt((String)stack.pop());
stack.push(a+b);
}else if(tokens[i]=="-"){
int a=Integer.parseInt((String)stack.pop());
int b=Integer.parseInt((String)stack.pop());
stack.push(a-b);
}else if(tokens[i]=="*"){
int a=Integer.parseInt((String)stack.pop());
int b=Integer.parseInt((String)stack.pop());
stack.push(a*b);
}else if(tokens[i]=="/"){
int a=Integer.parseInt((String)stack.pop());
int b=Integer.parseInt((String)stack.pop());
if(b==0){
return 0; }else{
stack.push(a/b);
} }else{
stack.push(tokens[i]);
} }
int result=(int)stack.peek();
return result;
}
public static void main(String[] args){
String[] tokens={"0","3","/"}; System.out.println(evalRPN(tokens));
}
}