kakasoo

[node.js] 2개 이하로 다른 비트 ( 프로그래머스 레벨2 ) 본문

프로그래밍/알고리즘 풀이

[node.js] 2개 이하로 다른 비트 ( 프로그래머스 레벨2 )

카카수(kakasoo) 2021. 7. 1. 14:36
반응형
// 프로그래머스 레벨2 2개 이하로 다른 비트를 풀었습니다.

const ImSoBoring = (num) => {
    const bits = num.toString(2).split("");
    if (num % 2 === 0) {
        bits.pop();
        return parseInt([...bits, "1"].join(""), 2);
    } else {
        bits.unshift("0");
        for (let i = bits.length - 1; i >= 0; i--) {
            if (bits[i] === "0") {
                bits[i] = "1";
                bits[i + 1] = "0";
                return parseInt(bits.join(""), 2);
            }
        }
    }
};

const solution = (numbers) => {
    return numbers.map((el) => ImSoBoring(el));
};
반응형