您的位置:首页 > 编程语言 > Java开发

[leetcode-338]Counting Bits(java)

2016-03-27 20:42 716 查看
原文地址:https://leetcode.com/problems/counting-bits/

分析:这道题要求给定N,然后计算从0到N之间所有的数的bit为1的数,并要求时间复杂度为O(N),因此对于每个数,然后求它的bit数的方法是不可取的。

所幸,当我们把数从0到N写出来时很容易发现规律,即对于2^N的数,末尾N-1位的重复规律,正好等于前N-1个数的重复规律,而这时只需要加1即可。

public class Solution {
public int[] countBits(int num) {
int[] res = new int[num + 1];
res[0] = 0;

int base = 1;//末尾为1的情况
while(base <= num){
int next = base * 2;//下一个末尾全为0的值
for(int i = base;i<next && i<=num;i++){
res[i] = res[i-base]+1;
}
base = next;
}
return res;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: