-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Lv_2 | ||
|문제명|푼 날짜|채점 결과|재응시여부| | ||
|---|:---:|:---:|:---:| | ||
|[올바른 괄호](./rightBracket.js)|22.06.04|O|X| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// 처음에 푼 방법 | ||
/* | ||
stack에 push하고 pop해서 풀었으나 시간초과 문제로 효율성이 낮게 나옴 | ||
*/ | ||
function solution(s){ | ||
let answer = false; | ||
let stack = []; | ||
if(s[0] === ')') return false; | ||
for(let x of s) { | ||
if(x === '(') stack.push('('); | ||
else stack.pop(); | ||
} | ||
} | ||
|
||
|
||
// 다시 푼 방법 | ||
/* | ||
stack을 이용하지 않고 단순히 for문에서 '('가 나오면 count를 1 증가하고, ')'가 나오면 count를 1 감소시킴 | ||
s의 시작이 ')'인 경우 count가 -1이 되므로 이 경우를 예외로 잡아줌 | ||
-> 채점 통과함 | ||
*/ | ||
function solution(s){ | ||
let answer = false; | ||
let count = 0; | ||
for(let x of s) { | ||
if(count < 0) return false; | ||
if(x === '(') count++; | ||
else if(x === ')')count--; | ||
} | ||
if(count === 0) return true; | ||
return answer; | ||
} |