标签:
题目链接:https://leetcode.com/problems/counting-bits/
题目:
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1‘s in their binary representation and return them as an array.
Example:
For num = 5
you should return [0,1,1,2,1,2]
.
Follow up:
算法:
public int[] countBits(int num) { int res[] = new int[num+1]; for(int i=0;i<=num;i++){ res[i] = res[i>>1]+(i&1); } return res; }
标签:
原文地址:http://blog.csdn.net/yeqiuzs/article/details/51520468