[leetcode, JS] 1009. Complement of Base 10 Integer

mxxn·2023년 9월 1일
0

leetcode

목록 보기
61/198

문제

문제 링크 : Complement of Base 10 Integer

풀이

/**
 * @param {number} n
 * @return {number}
 */
var bitwiseComplement = function(n) {
    if( n=== 0 ) return 1
    let chk = 1
    while(chk <= n) {
        chk *=2
    }
    return chk-1 -n
};
  1. n이 0일 때는 1 return
  2. chk 변수를 만들고 n보다 커질 때까지 x2
  3. chk-1 -n을 return
  • Runtime 47 ms, Memory 41.6 MB
profile
내일도 글쓰기

0개의 댓글