강의로 돌아가기
최한욱

1번 런타임 에러

1번만 에러가 나네요...

작성중인 코드―Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
class Solution {
    static int [] whereOpen;
    static int [] whereClose;
    static int [] whereLasers;
        public int solution(String arrangement) {
        int answer = 0;
        findMate(arrangement);
        findLasers(whereOpen, whereClose, arrangement);
        answer = howManyCut(arrangement);

        System.out.println(answer);
        return answer;
    }

    public static void findMate(String arrangement) {
        String open = "(";
        String close = ")";
        int openNumber = (arrangement.length())/2;

        int num_1 = 0;
        whereOpen = new int[openNumber];
        whereClose = new int [openNumber];

        for(int i = 0; i < arrangement.length() - 1; i++) {
            String str = String.valueOf(arrangement.charAt(i));
            if(str.equals(open)) {
                whereOpen[num_1] = i;
                num_1++;
            }
        }
        int num_3 = 0;
        for(int i = 0; i < whereOpen.length; i++) {
            int num_2 = whereOpen[i];
            int value = 1;
            for(int j = num_2 + 1; j < arrangement.length(); j++) {
                if((String.valueOf(arrangement.charAt(j))).equals(open)) {
                    value++;
                }
                else {
                    value--;
                }
                if(value == 0) {
                    whereClose[num_3] = j;
                    num_3++;
                    break;
                }
            }
        }

    }

    public static void findLasers (int[] whereOpen, int[] whereClose, String arrangement) {
        int num_1 = 0;
        int num_2 = 0;
        int openNumber = (arrangement.length())/2;
        for(int i = 0; i < openNumber; i++) {
            if(whereOpen[i] + 1 == whereClose[i]) {
                num_1++;
            }
        }
        whereLasers = new int [num_1]; 
        for(int i = 0; i < openNumber; i++) {
            if(whereOpen[i] + 1 == whereClose[i]) {
                whereLasers[num_2] = whereOpen[i];
                num_2++;
            }
        }
    }

    public static int howManyCut(String arrangement) {
        int answer = 0;
        int openNumber=  (arrangement.length())/2;
        for(int i = 0; i < openNumber; i++) {
            int front = whereOpen[i];
            int end = whereClose[i];
            int num_1 = 0;
            for(int j = 0; j < whereLasers.length; j++) {
                if(front < whereLasers[j] && end > whereLasers[j]) {
                    answer++;
                    num_1++;
                }
                if(j == whereLasers.length - 1 && num_1 != 0) answer++;
            }
        }
        return answer;
    }
}
0 개의 답변
답변 쓰기
이 입력폼은 마크다운 문법을 지원합니다. 마크다운 가이드 를 참고하세요.