[프로그래머스] 다음 큰 숫자

SeHoony·2022년 9월 13일
0

코테준비

목록 보기
24/27
function solution(n) {
    var answer = 0;
    let first_index = 0;
    let left_cnt = [];
    
    let target = n.toString(2)
    
    target.split('').map((e,i) => {
        if(target[i-1] === '0' && e === '1'){
            first_index = i
        }
        if(e === '1'){
            left_cnt.push(i)
        }
    })
    left_cnt = left_cnt.filter(e => e>first_index).length
    let length = target.slice(first_index+1).length
    let array = new Array(length).fill(0)
    for(let i = 0 ; i < left_cnt ; i++){
        array[i] = 1
    }
    
    array = array.reverse().join('')
    const cng = first_index === 0 ? '10' +  array : target.slice(0,first_index-1) +'10' +  array 
    console.log(parseInt(cng,2))
    answer = parseInt(cng,2)
    return answer;
}
function solution(n) {
    var answer = 0;
    const num_one = n.toString(2).split('1').length
    
    while(true){
        n++;
        if(n.toString(2).split('1').length === num_one){
            return n
        }
    }
    
    return answer;
}
profile
두 발로 매일 정진하는 두발자, 강세훈입니다. 저는 '두 발'이라는 이 단어를 참 좋아합니다. 이 말이 주는 건강, 정직 그리고 성실의 느낌이 제가 주는 분위기가 되었으면 좋겠습니다.

0개의 댓글