Skip to content

Commit 68de2d4

Browse files
committed
[level 2] Title: 올바른 괄호, Time: 22.09 ms, Memory: 55 MB -BaekjoonHub
1 parent 24ace6a commit 68de2d4

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

프로그래머스/2/12909. 올바른 괄호/README.md

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

55
### 성능 요약
66

7-
메모리: 55.1 MB, 시간: 6.67 ms
7+
메모리: 55 MB, 시간: 22.09 ms
88

99
### 구분
1010

@@ -16,7 +16,7 @@
1616

1717
### 제출 일자
1818

19-
2025년 08월 01일 12:00:57
19+
2025년 09월 01일 19:08:56
2020

2121
### 문제 설명
2222

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1+
import java.util.*;
2+
13
class Solution {
24
boolean solution(String s) {
3-
boolean answer = false;
4-
int count = 0;
5-
for(int i = 0; i<s.length();i++){
6-
if(s.charAt(i) == '('){
7-
count++;
8-
}
9-
if(s.charAt(i) == ')'){
10-
count--;
5+
Stack<Character> stack = new Stack<>();
6+
7+
String str = s.substring(1,s.length());
8+
stack.push(s.charAt(0));
9+
10+
for(char ch : str.toCharArray()){
11+
if(ch == ')'){
12+
if(!stack.isEmpty() && stack.pop() == '('){
13+
continue;
14+
}
15+
return false;
1116
}
12-
if(count < 0){
13-
break;
14-
}
15-
}
16-
if(count == 0){
17-
answer = true;
17+
stack.push(ch);
1818
}
19+
1920

20-
return answer;
21+
return stack.isEmpty() ? true : false;
2122
}
22-
}
23+
}

0 commit comments

Comments
 (0)