Evaluate the value of an arithmetic expression in Reverse Polish Notation.
Valid operators are+,-,*,/. Each operand may be an integer or another expression.
public class Solution {
public int evalRPN(String[] tokens) {
String[]str=tokens;
LinkedList<Integer> stack=new LinkedList<Integer>();
for(int i=0;i<str.length;i++){
if(str[i].equals("+")||str[i].equals("-")||str[i].equals("*")||str[i].equals("/")){
if(str[i].equals("+")){
int k2=stack.pop();
int k1=stack.pop();
int k=k1+k2;
stack.push(k);
}
if(str[i].equals("-")){
int k2=stack.pop();
int k1=stack.pop();
int k=k1-k2;
stack.push(k);
}
if(str[i].equals("*")){
int k2=stack.pop();
int k1=stack.pop();
int k=k1*k2;
stack.push(k);
}
if(str[i].equals("/")){
int k2=stack.pop();
int k1=stack.pop();
int k=k1/k2;
stack.push(k);
}
}else{
stack.push(Integer.parseInt(str[i]));
}
}
return stack.pop();
}
}