728x90
SMALL
안녕하세요 남갯입니다.
public static int solution(String S) {
// write your code in Java SE 8
if (S.length() % 2 == 1) {
return 0;
} else if (S.isEmpty()) {
return 1;
}
char[] array = S.toCharArray();
Stack<Character> stack = new Stack<>();
for (int i = 0; i < array.length; i++) {
if (array[i] == '{' || array[i] == '[' || array[i] == '(') {
stack.push(array[i]);
} else if (array[i] == '}') {
if (stack.empty() || stack.pop() != '{') {
return 0;
}
} else if (array[i] == ']') {
if (stack.empty() || stack.pop() != '[') {
return 0;
}
} else if (array[i] == ')') {
if (stack.empty() || stack.pop() != '(') {
return 0;
}
}
}
if(!stack.empty()){
return 0;
}
return 1;
}
728x90
LIST
'IT > 알고리즘' 카테고리의 다른 글
[코딜리티] - Lesson 7. Nesting (0) | 2019.09.09 |
---|---|
[코딜리티] - Lesson 7. Fish (0) | 2019.09.09 |
[코딜리티] - Lesson 6. Triangle (0) | 2019.09.02 |
[코딜리티] - Lesson 5. CountDiv (1) | 2019.08.27 |
[코딜리티] - Lesson 6. Distinct (0) | 2019.08.22 |