Is this a reasonable answer?


  • 0
    Z
    class Solution {
        func countBits(num: Int) -> [Int] 
        {
            let bits = [0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4];
            var count:Int = 0
            var answer:Array<Int> = []
            for idx in 0...num
            {
                var tmp = idx
                var mask = 15
                var more = 0
                repeat
                {
                    more += bits[tmp & mask]
                    tmp >>= 4
                } while tmp > 0
                answer.append(more)
            }
            return answer
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.