代码拉取完成,页面将自动刷新
同步操作将从 程序员二师兄/leetcode 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
import java.util.Stack;
/**
* @创建人 luoxiang
* @创建时间 2019/9/27 16:11
* @描述 682. 棒球比赛 https://leetcode-cn.com/problems/baseball-game/
*/
public class LeetCode_682 {
public static void main(String[] args) {
System.out.println(new LeetCode_682().calPoints(new String[]{"5", "2", "C", "D", "+"}));
System.out.println(new LeetCode_682().calPoints(new String[]{"5", "-2", "4", "C", "D", "9", "+", "+"}));
}
/** Method 1
* Created by LuoXiang on 2019/09/27 16:28
* Desc: 思路 —— 利用栈来保存每轮的操作。
* 复杂度: 时间复杂度:O(n) ; 空间复杂度:O(n)
**/
public int calPoints(String[] ops) {
Stack<Integer> stack = new Stack<>();
for (String op : ops) {
switch (op) {
case "+":
Integer last = stack.pop();
Integer lastTwo = stack.peek();
stack.push(last);
stack.push(last + lastTwo);
break;
case "D":
stack.push(stack.peek() * 2);
break;
case "C":
stack.pop();
break;
default:
stack.push(Integer.valueOf(op));
}
}
int sum = 0;
while (!stack.empty()) {
sum += stack.pop();
}
return sum;
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。