96. count "1" in binary form

JavaScript
Is this helpful ?

easy  - accepted / - tried

Given an integer, count "1" in its binary form.

countOne(1) // 1,  "1"
countOne(257799) // 12, "111110111100000111"
  1. If you use built-in string methods in JavaScript, please do understand the time complexity, they are not free.
  2. Actually this could be easily done by counting the digit one by one. Could you think of some other approaches?

Let's try to solve this problem within 10 mintues.

Discuss(19)