괄호 문자 제거(Stack)
- My Solution 1
- Answer
- In Progress
function solution(s) {
let stack = [];
let cnt = 0;
for(let i=0; i<s.length; i++) {
if(s[i] === ')') {
while(cnt > 0) {
stack.pop();
cnt--;
console.log('pop', cnt, s[i]);
}
} else {
stack.push(s[i]);
cnt++;
console.log('push', cnt, s[i]);
}
}
return stack;
}
let str="(A(BC)D)EF(G(H)(IJ)K)LM(N)";
console.log(solution(str));
- output
push 1 (
push 2 A
push 3 (
push 4 B
push 5 C
pop 4 )
pop 3 )
pop 2 )
pop 1 )
pop 0 )
push 1 D
pop 0 )
push 1 E
push 2 F
push 3 (
push 4 G
push 5 (
push 6 H
pop 5 )
pop 4 )
pop 3 )
pop 2 )
pop 1 )
pop 0 )
push 1 (
push 2 I
push 3 J
pop 2 )
pop 1 )
pop 0 )
push 1 K
pop 0 )
push 1 L
push 2 M
push 3 (
push 4 N
pop 3 )
pop 2 )
pop 1 )
pop 0 )
Author And Source
이 문제에 관하여(괄호 문자 제거(Stack)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@reveloper-1311/괄호-문자-제거저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)