알고리즘 풀이/Codility

Codility - Nesting (Stacks and Queues)

배게 2019. 12. 13. 15:22
728x90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    public int solution(String S) {
        // write your code in Java SE 8
        
        Stack<Character> st = new Stack<>();
        
        for(int i=0; i<S.length(); i++){
            switch(S.charAt(i)){
                case '(' :
                    st.push('(');
                    break;
                case ')' :
                    if(st.isEmpty()) return 0;
                    st.pop();
                    break;
            }
            
        }
        if(st.isEmpty()) return 1;
        else return 0;
    }
cs