网站设计书籍,模板做图 网站有哪些,wordpress页面中添加小工具栏,做网站的人叫什么338. 比特位计数
给你一个整数 n #xff0c;对于 0 i n 中的每个 i #xff0c;计算其二进制表示中 1 的个数 #xff0c;返回一个长度为 n 1 的数组 ans 作为答案。 今天看一下这道简单题#xff0c;主要考查位运算#xff0c;代码如下
class Solution {pu…338. 比特位计数
给你一个整数 n 对于 0 i n 中的每个 i 计算其二进制表示中 1 的个数 返回一个长度为 n 1 的数组 ans 作为答案。 今天看一下这道简单题主要考查位运算代码如下
class Solution {public int[] countBits(int n) {int[] bits new int[n 1];int heightBit 0;for (int i 1; i n; i) {if ((i (i - 1)) 0) {heightBit i;}bits[i] bits[i - heightBit] 1;}return bits;}
}
题目链接题单 - 力扣LeetCode全球极客挚爱的技术成长平台