Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func countBits(_ num: Int) -> [Int] {
- guard num > 0 else { return [0] }
- var dp = [Int](repeating: 0, count: num + 1)
- dp[1] = 1
- dp[0] = 0
- for i in stride(from: 2, through: num, by: 1){
- dp[i] = i % 2 + dp[i / 2]
- }
- return dp
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement