본문 바로가기
Problem Solving/LeetCode

[LeetCode] 682. Baseball Game - Java

by graycode 2026. 9. 23.

• 문제 링크

 

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;
    }

}

댓글