懒人速刷之java栈和队列
栈和队列
简介
基本应用
逆波兰表达式求值
public int evalRPN(String[] tokens) {
Stack<Integer> stack = new Stack<>();
for (String s : tokens) {
if ("+".equals(s) || "-".equals(s) || "*".equals(s) || "/".equals(s)) {
int a = stack.pop();
int b = stack.pop();
if ("+".equals(s)) stack.push(b + a);
else if ("-".equals(s)) stack.push(b - a);
else if ("*".equals(s)) stack.push(b * a);
// 注意:b为被除数,a为除数
else if ("/".equals(s)) stack.push(b / a);
} else {
// 转为数字
stack.push(Integer.parseInt(s));
}
}
return stack.pop();
}有效的括号
单调栈
柱状图中最大的矩形
接雨水
设计类问题
最后更新于