LeetCode: Counting Bits Solution
Convert to binary with toString() methodApproach
Convert number to binary string using .toString(2)
Count the 1s
Implementation
1var countBits = function (n) {2 return Array.from(3 { length: n + 1 },4 (_, i) => i.toString(2).replace(/0/g, "").length5 )6}
References
Comments
Loading comments...
Tags
leetcode
string
bit manipulation
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.
Next Post
LeetCode: Longest Palindrome
Trim odds
Previous Post
LeetCode: Is Subsequence
The author is not smart enough to come up with the DP solution in the first place