일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 프로그래머스
- DP
- 정렬
- 우선순위큐
- 그리디알고리즘
- SQL
- select
- 깊이우선탐색
- Spring
- springboot
- Database
- mariaDB
- BFS
- 다이나믹프로그래밍
- 데이터베이스
- Effective Java
- DFS
- 백준
- join
- mybatis
- 탐욕법
- 이펙티브자바
- 알고리즘
- db
- 코테
- IntelliJ
- java
- 너비우선탐색
- Greedy
- 피보나치
Archives
- Today
- Total
땀두 블로그
[백준] 9012번 - 괄호 본문

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class p9012 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int a = Integer.parseInt(br.readLine());
String[] ary = new String[a];
for (int i = 0; i < a; i++) {
ary[i] = br.readLine();
}
for (int i = 0; i < a; i++) {
int cnt = 0;
boolean flag = false;
for (int j = 0; j < ary[i].length(); j++) {
if (ary[i].charAt(j) == '(') {
cnt++;
} else {
cnt--;
}
if (cnt < 0) {
flag = true;
}
}
if (cnt == 0 && flag == false) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
}
}
괄호의 개수를 cnt에 담아 (가 나올때 더해주고 ) 가 나올 때 빼주는 간단한 문제라고 생각했는데 )가 먼저 나오는 경우는 VPS가 성립되지 않기 때문에 이러한 점을 구분하기 위해 flag 변수를 두었고, 이런 경우는 cnt가 음수가 되는 값이므로 조건을 추가해주었다.
'알고리즘 > 백준' 카테고리의 다른 글
[백준] 10828번 - 스택 (0) | 2022.03.20 |
---|---|
[백준] 10773번 - 제로 (0) | 2022.03.20 |
[백준] 1920번 - 수 찾기(HashSet) (0) | 2022.03.20 |
[백준] 2108번 - 통계학 (0) | 2022.03.20 |
[백준] 1920번 - 수 찾기 (0) | 2022.03.20 |
Comments