Algoritm/Quiz-Solutions

[백준 9012] 괄호(Java) - Stack

💜autumn 2020. 5. 4. 00:58

[문제링크]

https://www.acmicpc.net/problem/9012

 

[풀이소스]

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
package algorithm;
 
 
public class Stack9012 {
    public static void main(String[] args) throws NumberFormatException, IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int testcase=Integer.parseInt(br.readLine());
        StringBuilder sb=new StringBuilder();
        
        while(testcase-->0) {
            String str=br.readLine();
            boolean isVps=true;
            Stack<Character> marks=new Stack<Character>();
            
            for(int i=0;i<str.length();i++) {
                char temp=str.charAt(i);
                if(temp=='(') {
                    marks.push('(');
                } else {
                    if(marks.isEmpty()) {
                        isVps=false;
                        break;
                    }
                    marks.pop();
                }
            }
            if (isVps && marks.isEmpty()) {
                sb.append("YES \n");
            } else {
                sb.append("NO \n");
            }
        }
        System.out.println(sb);
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

 

[느낀점]

   - 변수선언하는 위치 제발 제발 주의!

   - 괄호문제는 Stack!이용, 나는 여는 괄호를 넣었는데 닫는괄호넣는게 여러괄호있는경우에는 편하다고 하더라?

   - 처음에 isVps를 String으로 활용했는데 그건 왜안되는걸까? 우선 boolean형태로 하니까 OK! 

      이유가 뭐지..