File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed
minjeong/Stack, Queue, Priority Queue Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments