Skip to content

Commit 37ac82d

Browse files
committed
[Gold V] Title: 괄호의 값, Time: 104 ms, Memory: 14376 KB -BaekjoonHub
1 parent cd61f76 commit 37ac82d

2 files changed

Lines changed: 51 additions & 3 deletions

File tree

백준/Gold/2504. 괄호의 값/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
### 성능 요약
66

7-
메모리: 32412 KB, 시간: 40 ms
7+
메모리: 14376 KB, 시간: 104 ms
88

99
### 분류
1010

11-
자료 구조, 구현, 스택
11+
구현, 자료 구조, 스택
1212

1313
### 제출 일자
1414

15-
2025년 4월 2일 20:00:38
15+
2025년 10월 24일 10:47:23
1616

1717
### 문제 설명
1818

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.util.*;
5+
6+
public class Main {
7+
8+
public static void main(String[] args) throws IOException {
9+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10+
String line = br.readLine();
11+
12+
Stack<Character> stack = new Stack<>();
13+
int res = 0;
14+
int value = 1;
15+
16+
for (int i = 0; i < line.length(); i++) {
17+
if (line.charAt(i) == '(') {
18+
stack.push(line.charAt(i));
19+
value *= 2;
20+
} else if (line.charAt(i) == '[') {
21+
stack.push(line.charAt(i));
22+
value *= 3;
23+
} else if (line.charAt(i) == ')') {
24+
if (stack.isEmpty() || stack.peek() != '(') {
25+
res = 0;
26+
break;
27+
} else if (line.charAt(i - 1) == '(') {
28+
res += value;
29+
}
30+
stack.pop();
31+
value /= 2;
32+
} else if (line.charAt(i) == ']') {
33+
if (stack.isEmpty() || stack.peek() != '[') {
34+
res = 0;
35+
break;
36+
} else if (line.charAt(i - 1) == '[') {
37+
res += value;
38+
}
39+
40+
stack.pop();
41+
value /= 3;
42+
}
43+
}
44+
45+
if (!stack.isEmpty()) System.out.println(0);
46+
else System.out.println(res);
47+
}
48+
}

0 commit comments

Comments
 (0)