zhning12 / Coding-Interviews

The Questions, Analysis and Solution of 《Coding Interviews: Questions, Analysis and Solution》

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

11. 二进制中1的个数

yangxiaolin opened this issue · comments

这个问题可以用一个取巧得方式实现。

   function NumberOf1(n) {
       return n.toString(2)
           .split('')
           .reduce((count, current) =>  current === '1' ? count + 1 : count, 0)
  }

效率比你实现得差点