Middle-题目130：338. Counting Bits（增补2）

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].

public class Solution {
public int[] countBits(int num) {
int[] bits = new int[num+1];
for(int i = 0;i<=num;i++)
bits[i] = Integer.bitCount(i);
return bits;
}
}

5ms（统计信息还未显示）
Cmershen的碎碎念：

public static int bitCount(int i) {
// HD, Figure 5-2
i = i - ((i >>> 1) & 0x55555555);
i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
i = (i + (i >>> 4)) & 0x0f0f0f0f;
i = i + (i >>> 8);
i = i + (i >>> 16);
return i & 0x3f;
}

• 本文已收录于以下专栏：

leetcode_middle_1_338. Counting Bits

• pusude
• 2017年01月24日 20:15
• 118

[LC338] Counting Bits

Total Accepted: 29725 Total Submissions: 52050 Difficulty: Medium Given a non negative ...

【LeetCode】338. Counting Bits

• mrbcy
• 2017年03月15日 21:27
• 498

Leetcode: 338.Counting Bits

Leetcode: 338.Counting Bits

leetcode(338). Counting Bits

problem Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num c...

LeetCode中338Counting Bits的题解

举报原因： 您举报文章：Middle-题目130：338. Counting Bits（增补2） 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)