LC 383. Counting Bits

/**
 * @param {number} n
 * @return {number[]}
 */
var countBits = function (n) {
  const bits = new Array(n + 1).fill(0)
  for (let i = 0; i <= n; i++) {
    bits[i] = countOnes(i)
  }
  return bits
};
const countOnes = (x) => {
  let ones = 0
  while (x > 0) {
    x &= (x - 1)
    ones++
  }
  return ones
}
欢迎通过「邮件」或者点击「这里」告诉我你的想法
Welcome to tell me your thoughts via "email" or click "here"