2019年1月23日星期三

[LeetCode] 338. Counting Bits

要累晕啦,做一道简单的,本来想用一个循环来数一个整型的二进制有多少个1,但是超时了。只能用个投机取巧的方法了……
查了一下Integer.bitCount()的实现,肯定是记不住的啊ㄟ(▔▽▔)ㄏ
class Solution {
public int[] countBits(int num) {
int[] result = new int[num + 1];
for (int i = 0; i <= num; i++) {
result[i] = Integer.bitCount(i);
}
return result;
}
}
view raw countBits.java hosted with ❤ by GitHub

没有评论:

发表评论