import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param tokens string字符串一维数组
* @return int整型
*/
public int evalRPN(String[] tokens) {
// write code here
int res = 0; // 定义一个整型变量,用于存放最终的返回结果
int num1 = 0;
int num2 = 0;
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < tokens.length; i++) {
String token = tokens[i];
switch (token) {
case "+":
num1 = stack.pop();
num2 = stack.pop();
res = num2 + num1;
stack.push(res);
break;
case "-":
num1 = stack.pop();
num2 = stack.pop();
res = num2 - num1;
stack.push(res);
break;
case "*":
num1 = stack.pop();
num2 = stack.pop();
res = num2 * num1;
stack.push(res);
break;
case "/":
num1 = stack.pop();
num2 = stack.pop();
res = num2 / num1;
stack.push(res);
break;
default:
stack.push(Integer.valueOf(token));
}
}
res = stack.pop();
return res;
}
}