
LeetCode(338)--比特位计数
发布日期:2021-05-10 07:48:50
浏览次数:18
分类:精选文章
本文共 1790 字,大约阅读时间需要 5 分钟。
��������������������������������������� num��������� 0 ��� num ������������������������������������ 1 ������������������������������������������������
��������������������������������������� num+1 ��������� result��������������������������� i ��������������� 1 ������������
���������������
- result[0] = 0��������� 0 ��������������������������� 1���
- ������ num >= 1���result[1] = 1��������� 1 ��������������������������� 1 ��� 1���
���������������������
- ��� i=2 ��� num��������������������������� i���
- ������ i ������������result[i] = result[i/2]������������������������������ 0������
- ������ i ������������result[i] = result[i/2] + 1������������������������������ 1������
��������������������������������� O(n)������������������������������������������������������������ O(n)������������������������������������������������������
���������������
class Solution { public int[] countBits(int num) { int[] result = new int[num + 1]; if (num == 0) { result[0] = 0; return result; } result[0] = 0; result[1] = 1; for (int i = 2; i <= num; i++) { if (i % 2 == 0) { result[i] = result[i / 2]; } else { result[i] = result[i / 2] + 1; } } return result; }}
���������
- ������������������O(n)��������������������������������������������������� 2 ��� num���
- ������������������O(n)������������������������������
- ������������������������������������������������������������������������������������������������������������������������������������
- ������������������������������������������������������������������������������������������������������������
���������������������������������������������������������������������������������������
发表评论
最新留言
留言是一种美德,欢迎回访!
[***.207.175.100]2025年04月10日 03时00分58秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
重定向
2021-05-10
08-springmvc-异常解析器
2021-05-10
杂谈: 记一次深夜发版经历
2021-05-10
在select后面嵌套子查询
2021-05-10
表的复制和批量插入
2021-05-10
MYISAM存储引擎
2021-05-10
练习题第一道
2021-05-10
什么情况必须使用 statement
2021-05-10
账号转账演示事务
2021-05-10
HDML BS结构和CS结构介绍
2021-05-10
Object类:jDK类库的根类
2021-05-10
java中的集合回顾-collections工具类进行一个集合排序
2021-05-10
maven maven知识点回顾
2021-05-10
VS VS导入opencv的配置文件到Debug文件后还是无法导入库函数
2021-05-10
idea创建工程时错误提醒的是architectCatalog=internal
2021-05-10
E - Another Postman Problem FZU - 2038
2021-05-10
力扣 1658. 将 x 减到 0 的最小操作数
2021-05-10
图解redis(二)
2021-05-10
input type="checkbox" 样式美化
2021-05-10
【Java】 # 对于日期Date类的相关操作
2021-05-10