Skip to content

Commit 4ff7262

Browse files
committed
Valid_parentheses / 중급
1 parent 693370b commit 4ff7262

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function solution(s) {
2+
let stack = []; // 스택
3+
let arr = s.split(""); // 괄호 별로 잘라서 배열 생성
4+
for (let i = 0; i < arr.length; i++) {
5+
if (arr[i] === "(") {
6+
// 열린 괄호일때
7+
stack.push(arr[i]); // 스택에 열린 괄호 추가
8+
} else {
9+
// 닫힌 괄호일때
10+
stack.pop(); // 스택에 쌓아둔 열린 괄호 제거
11+
}
12+
}
13+
if (stack.length === 0) {
14+
// 스택이 비어있으면 짝이 맞다는 뜻이므로 length가 0
15+
return true; // 결과 반환
16+
} else {
17+
return false;
18+
}
19+
}

0 commit comments

Comments
 (0)