• 문제 링크
Baseball Game - LeetCode
Can you solve this real interview question? Baseball Game - You are keeping the scores for a baseball game with strange rules. At the beginning of the game, you start with an empty record. You are given a list of strings operations, where operations[i] is
leetcode.com
• 풀이 코드
public class Solution {
public int calPoints(String[] operations) {
int[] stk = new int[operations.length];
int top = -1;
for (String o : operations) {
switch (o) {
case "+" -> stk[++top] = stk[top - 1] + stk[top - 2];
case "D" -> stk[++top] = stk[top - 1] * 2;
case "C" -> top--;
default -> stk[++top] = Integer.parseInt(o);
}
}
int sum = 0;
for (int i = 0; i <= top; i++) sum += stk[i];
return sum;
}
}'Problem Solving > LeetCode' 카테고리의 다른 글
| [LeetCode] 696. Count Binary Substrings - Java (0) | 2026.09.25 |
|---|---|
| [LeetCode] 693. Binary Number with Alternating Bits - Java (0) | 2026.09.24 |
| [LeetCode] 680. Valid Palindrome II - Java (0) | 2026.09.22 |
| [LeetCode] 674. Longest Continuous Increasing Subsequence - Java (0) | 2026.09.21 |
| [LeetCode] 671. Second Minimum Node In a Binary Tree - Java (0) | 2026.09.20 |
댓글