timber0308   2년 전

Occured : NumberFormat Error

이 코드가 안되니까 심적으로 많이 힘드네요

사람 살려주세요.

yt_dyk   2년 전

  1. 데이터 타입의 차이: 두 코드 모두 괄호 안의 숫자 합을 나타내는 변수를 long 형으로 선언하고 있습니다. 그러나 두 코드에서는 숫자를 처리하는 부분에서 데이터 타입이 차이가 있습니다.

    원래 코드:

    java복사<code>result += Integer.parseInt(cur);
    </code>

    정답 코드:

    java복사<code>result += Long.parseLong(cur);
    </code>

    첫 번째 코드에서는 Integer.parseInt()를 사용하여 숫자를 처리하고 있고, 두 번째 코드에서는 Long.parseLong()를 사용하고 있습니다. 숫자의 크기가 일정 범위를 벗어날 경우에 영향을 미칠 수 있습니다.  문제의 입력 범위에 따라서 long이 필요한 경우와 아닌 경우가 있을 수 있습니다.

  2. 괄호를 pop할 때의 숫자 합 초기화: 두 코드에서 괄호를 pop할 때의 숫자 합을 초기화하는 부분에 차이가 있습니다.

    원래 코드:

    java복사<code>result += Integer.parseInt(cur);
    
    

    정답 코드:

    java복사<code>result += Long.parseLong(cur);숫자 합이 0일 때 초기값을 어떻게 설정하느냐에 따라 결과가 달라질 수 있습니다.
    

그래도 혹시 쩨가 코드를 바꾸면서 빼먹은 부분이 있을 수도 있으니 제가 님 코드를 바꾼 정답 코드도 올려드리겠습니다

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayDeque;

public class Main {

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String str = br.readLine();
        String[] temp = str.split("");

        ArrayDeque<String> stack = new ArrayDeque<>();

        try {
            for (int i = 0; i < str.length(); i++) {
                String cur = temp[i];

                if (cur.equals("(") || cur.equals("[")) {
                    stack.add(cur);
                } else if (cur.equals(")")) {
                    long sum = 0;

                    while (!stack.isEmpty() && !stack.peekLast().equals("(")) {
                        sum += Long.parseLong(stack.pollLast());
                    }

                    if (stack.isEmpty()) {
                        System.out.println(0);
                        return;
                    }

                    stack.pollLast();  // pop the '('
                    stack.add(String.valueOf((sum == 0) ? 2 : sum * 2));

                } else if (cur.equals("]")) {
                    long sum = 0;

                    while (!stack.isEmpty() && !stack.peekLast().equals("[")) {
                        sum += Long.parseLong(stack.pollLast());
                    }

                    if (stack.isEmpty()) {
                        System.out.println(0);
                        return;
                    }

                    stack.pollLast();  // pop the '['
                    stack.add(String.valueOf((sum == 0) ? 3 : sum * 3));
                }
            }

            int result = 0;
            while (!stack.isEmpty()) {
                String cur = stack.pollLast();

                if (cur.equals("(") || cur.equals("[") || cur.equals(")") || cur.equals("]")) {
                    System.out.println(0);
                    return;
                } else {
                    result += Integer.parseInt(cur);
                }
            }

            System.out.println(result);

        } catch (NumberFormatException e) {
            System.out.println(0);
        }
    }
}

힘 내시길 바랍니다.

yt_dyk   2년 전

맞으셨네요! 축하합니다!

댓글을 작성하려면 로그인해야 합니다.