백준 1181번, 1259번, 1436번 js 풀이

Kimseongeun·2022년 4월 7일
0

백준

목록 보기
2/3

백준 1181번

1181번: 단어 정렬

// const fs = require('fs');
// const input = fs.readFileSync('/dev/stdin').toString().trim().split('\n');

const input = [
    '13',
    'but',
    'i',
    'wont',
    'hesitate',
    'no',
    'more',
    'no',
    'more',
    'it',
    'cannot',
    'wait',
    'im',
    'yours'
];
const sorted = input.sort((a, b) => a.length - b.length || a.localeCompare(b));
const uniqueValues = new Set(sorted);
console.log(Array.from(uniqueValues).join('\n'));

✅솔루션

sort()로 단어의 길이만큼 정렬하고, 같은 길이의 단어가 있다면 localeCompare()를 이용해 정렬이 가능함.
그렇게 정렬된 배열을 new Set()에 됨

  • localeCompare : 브라우저 언어 설정 기반해 순서 비교
    ->str1이 str2 앞에 : -1 반환
    ->두 문자열 같으면 : 0 반환
    ->str2가 str1 앞에: 1 반환

    Set : 이 배열에 어떤 숫자가 포함 되어 있는지 확인가능 has()로 / Array.from으로 Arr 배열 객체로 변환 가능


백준 1259번

1259번: 팰린드롬수

let fs = require("fs");
let input = fs.readFileSync("/dev/stdin").toString().trim().split("\n");
input.pop(); //0이 포함되지 않는다고 나와있기에 마지막꺼 빼기

for (let i = 0; i < input.length; i++) {
    let reverseStr = input[i].split("").reverse().join("");
    console.log(input[i] === reverseStr ? "yes" : "no");
}

✅솔루션

어쨌든 앞뒤가 같아야하기 때문에 reverse 메서드를 이용해서 하나하나 짤라준 후 뒤집고 join으로 붙여서 처음과 같은지 비교를 하는 것이다!

정말 간단한 문제!


백준 1436번

1436번: 영화감독 숌

// const fs = require('fs');
// let input = parseInt(fs.readFileSync('/dev/stdin').toString().trim());
let input=3;
let num = 666;
let count = 1;
while (count !== input) {
    num++; if (String(num).includes("666")) count++;
}
console.log(num);

✅솔루션

num을 1씩 늘려가면서 그 값에 연속된 666이 존재하면(includes 사용) 그때마다 count를 증가시켜서 목표한 count(n)에 도달할때 까지 반복한다.

profile
김성은입니다.

0개의 댓글