思路:所有的数字存入list,读到符号取出后两个运算完放回去,最后返回list第一个值。
public class Solution {
public int evalRPN(String[] tokens) {
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < tokens.length; i++) {
if (tokens[i].equals("+")) {
int b = list.get(list.size() - 1);
list.remove(list.size() - 1);
int a = list.get(list.size() - 1);
list.remove(list.size() - 1);
int sum = a + b;
list.add(sum);
} else if (tokens[i].equals("-")) {
int b = list.get(list.size() - 1);
list.remove(list.size() - 1);
int a = list.get(list.size() - 1);
list.remove(list.size() - 1);
int sum = a - b;
list.add(sum);
} else if (tokens[i].equals("*")) {
int b = list.get(list.size() - 1);
list.remove(list.size() - 1);
int a = list.get(list.size() - 1);
list.remove(list.size() - 1);
int sum = a * b;
list.add(sum);
} else if (tokens[i].equals("/")) {
int b = list.get(list.size() - 1);
list.remove(list.size() - 1);
int a = list.get(list.size() - 1);
list.remove(list.size() - 1);
int sum = a / b;
list.add(sum);
} else {
list.add(Integer.parseInt(tokens[i]));
}
}
return list.get(0);
}
}
耗时:368ms,中下游,写的比较冗余