Study/알고리즘
[백준] 10828 스택 (JavaScript)
ujam
2021. 9. 5. 20:05
728x90
반응형
const { stdin } = require('process');
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('line', (line) => {
const input = line.split(/\r?\n/);
let stack = [];
counter = Number(input[0]);
for (let i = 0; i < counter + 1; i++) {
const command = input[i].split(' ');
if (command.length == 1) {
if (command[0] === 'pop') {
if (stack.length == 0) {
console.log(-1);
} else {
console.log(stack[stack.length - 1]);
stack.pop();
}
}
if (command[0] === 'size') {
console.log(stack.length);
}
if (command[0] === 'empty') {
if (stack.length == 0) {
console.log(1);
} else {
console.log(0);
}
}
if (command[0] === 'top') {
if (stack.length == 0) {
console.log(-1);
} else {
console.log(stack[stack.length - 1]);
}
}
} else {
Push = command[1];
stack.push(Push);
}
}
rl.close();
}).on('close', () => {
process.exit();
})
위 코드로는 로컬 환경에서 확인했습니다.
let fs = require('fs');
let input = fs.readFileSync('/dev/stdin').toString().split('\n');
let stack = [];
counter = Number(input[0]);
for (let i = 0; i < counter + 1; i++) {
const command = input[i].split(' ');
if (command.length == 1) {
if (command[0] === 'pop') {
if (stack.length == 0) {
console.log(-1);
} else {
console.log(stack[stack.length - 1]);
stack.pop();
}
}
if (command[0] === 'size') {
console.log(stack.length);
}
if (command[0] === 'empty') {
if (stack.length == 0) {
console.log(1);
} else {
console.log(0);
}
}
if (command[0] === 'top') {
if (stack.length == 0) {
console.log(-1);
} else {
console.log(stack[stack.length - 1]);
}
}
} else {
Push = command[1];
stack.push(Push);
}
}
위 코드로 백준 알고리즘 제출했습니다.
항상 부족한 부분이나 피드백할 부분을 댓글로 남겨주시면 적극적으로 수용하여 수정하겠습니다.
728x90
반응형