[LeetCode] 1009. Complement of Base 10 Integer

Chobby·2025년 6월 11일
1

LeetCode

목록 보기
445/479

😎풀이

  1. n을 2진수 형태로 변환
  2. 2진수 형태의 결과를 보수의 형태로 변환
  3. 해당 결괏값을 10진수 형태로 변환하여 반환
function bitwiseComplement(n: number): number {
    const arr = [...n.toString(2)]
    const complement = arr.map(a => a === '1' ? '0' : '1')
    const joined = complement.join('')
    return parseInt(joined, 2)
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글