728x90
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 | public int solution(String S) { // write your code in Java SE 8 System.out.println(S); Stack<Character> st = new Stack<>(); for(int i=0; i<S.length(); i++){ char c = S.charAt(i); switch (c){ case '{': case '[': case '(' : st.push(c); break; case '}' : if(st.isEmpty()) return 0; if(st.peek()=='{') st.pop(); else return 0; break; case ']' : if(st.isEmpty()) return 0; if(st.peek()=='[') st.pop(); else return 0; break; case ')' : if(st.isEmpty()) return 0; if(st.peek()=='(') st.pop(); else return 0; break; } } if(st.isEmpty()) return 1; else return 0; } | cs |
'알고리즘 풀이 > Codility' 카테고리의 다른 글
Codility - Nesting (Stacks and Queues) (0) | 2019.12.13 |
---|---|
Codility - Fish (Stacks and Queues) (0) | 2019.12.13 |
Codility - Triangle (Sorting) (0) | 2019.12.13 |
Codility - Distinct (Sorting) (0) | 2019.12.13 |
codility - MaxProductOfThree (Sorting) (0) | 2019.12.13 |