[LeetCode] 3370. Smallest Number With All Set Bits

Chobby·5일 전

LeetCode

목록 보기
899/912

😎풀이

  1. n을 2진수로 변환
  2. 모든 비트를 0 -> 1로 설정
  3. 변환된 비트를 10진수로 변환하여 반환
function smallestNumber(n: number): number {
    const binaryN = n.toString(2)
    const setBitN = [...binaryN].map(char => '1').join('')
    return parseInt(setBitN, 2)
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글