Advertisement
HXXXXJ

338. Counting Bits

Feb 18th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.30 KB | None | 0 0
  1.     func countBits(_ num: Int) -> [Int] {
  2.         guard num > 0 else { return [0] }
  3.         var dp = [Int](repeating: 0, count: num + 1)
  4.         dp[1] = 1
  5.         dp[0] = 0
  6.         for i in  stride(from: 2, through: num, by: 1){
  7.             dp[i] = i % 2 + dp[i / 2]
  8.         }
  9.         return dp
  10.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement