Leetcode 338. 比特位计数(DAY 25高数线代终于考完)----动态规划学习期
发布日期:2021-06-30 22:24:29 浏览次数:2 分类:技术文章

本文共 944 字,大约阅读时间需要 3 分钟。

原题题目

在这里插入图片描述



代码实现(大部分自解小部分看解)

/** * Note: The returned array must be malloced, assume caller calls free(). */int* countBits(int num, int* returnSize){
(*returnSize) = num+1; int i,count,temp; int* ret = (int*)malloc(sizeof(int) * (num+1)); for(i=0;i<=num;i++) {
count = 0; if(!i) ret[i] = i; else {
temp = i; while(temp) {
temp &= (temp-1); count++; } ret[i] = count; } } return ret;}

代码实现(二刷还是不会解 看首解)

class Solution {
public: vector
countBits(int num) {
vector
ret; ret.push_back(0); for(int i=1;i<=num;++i) {
int temp = i,count = 0; while(temp) {
temp &= (temp-1); ++count; } ret.push_back(count); } return ret; }};

转载地址:https://love6.blog.csdn.net/article/details/112785038 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Leetcode 1402. 做菜顺序(DAY 25 Hard 含题解)----动态规划学习期
下一篇:Leetcode 1641. 统计字典序元音字符串的数目(DAY 24 动态规划开始 ---鸽子一星期准备考试)----动态规划学习期

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月19日 06时33分00秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章