题目考察的知识点是:
本题主要考察知识点是栈、后缀表达式 。
题目解答方法的文字分析:
将集合中的数据循环判断是什么,然后再去计算结果即可。
本题解析所用的编程语言:
java语言。
完整且正确的编程代码:
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param tokens string字符串一维数组
* @return int整型
*/
public int calculatePostfix (String[] tokens) {
// write code here
Deque<Integer> stack = new ArrayDeque<>();
for (String token : tokens) {
Integer res = 0;
if (token.equals("+")) {
res = stack.pop() + stack.pop();
} else if (token.equals("-")) {
int digit = stack.pop();
res = stack.pop() - digit;
} else if (token.equals("*")) {
res = stack.pop() * stack.pop();
} else if (token.equals("/")) {
int digit = stack.pop();
res = stack.pop() / digit;
} else {
res = Integer.parseInt(token);
}
stack.push(res);
}
return stack.peek();
}
}

京公网安备 11010502036488号