Skip to content

Commit 8612d03

Browse files
committed
[PGS] 올바른 괄호 / 실버2 / 40분 / 자바 풀이 / 힌트, 성공
1 parent 60a1efb commit 8612d03

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
boolean solution(String s) {
5+
// "(" 이면 stack에 push. ")"이면 stack에서 pop하기
6+
// stack의 길이가 0이면 true, stack의 길이가 1이상이면 false
7+
8+
// Stack ArrayList
9+
ArrayList<Integer> stack = new ArrayList<>();
10+
11+
// 문자열을 ArrayList로 변환
12+
String[] stringParam = s.split("");
13+
ArrayList<String> list = new ArrayList<String>(Arrays.asList(stringParam));
14+
15+
// Stack Push & Pop
16+
for (String str: stringParam){
17+
// System.out.println(str);
18+
if (str.equals("(")) {
19+
stack.add(0);
20+
// System.out.println("추가 완료");
21+
}
22+
else {
23+
// 처음부터 ) 가 나올 경우 올바르지 않은 괄호
24+
if (stack.size() == 0){
25+
return false;
26+
}
27+
else {
28+
stack.remove(stack.size() - 1);
29+
}
30+
}
31+
}
32+
33+
// 올바른 괄호가 아닐 경우
34+
if (stack.size() > 0) {
35+
return false;
36+
}
37+
// 올바른 괄호가 아닌 경우
38+
return true;
39+
}
40+
}

0 commit comments

Comments
 (0)