count "1" in binary form
2022年5月1日小于 1 分钟
count "1" in binary form
Question
Given an integer, count "1" in its binary form.
countOne(1) // 1, "1"
countOne(257799) // 12, "111110111100000111"
- If you use built-in string methods in JavaScript, please do understand the time complexity, they are not free.
- Actually this could be easily done by counting the digit one by one. Could you think of some other approaches?
Code
/**
* @param {number} num - integer
* @return {number} count of 1 bit
*/
function countOne(num) {
// your code here
}