【LeetCode】338. Counting Bits 解题报告(Python & Java & C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目描述
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]
.
Follow up:
- It is very easy to come up with a solution with run time O(n*sizeof(integer)). But can you do it in linear time O(n) /possibly in a single pass?
- Space complexity should be O(n).
- Can you do it like a boss? Do it without using any builtin function like __builtin_popcount in c++ or in any other language.
题目大意
计算0<=x<=num的所有数字,二进制表示里面的1的个数。
解题方法
这个题用DP的方法。
分析规律:
0000 0
-------------
0001 1
-------------
0010 1
0011 2
-------------
0100 1
0101 2
0110 2
0111 3
-------------
1000 1
1001 2
1010 2
1011 3
1100 2
1101 3
1110 3
1111 4
把第i个数分成两种情况,如果i是偶数那么,它的二进制1的位数等于i/2中1的位数;如果i是奇数,那么,它的二进制1的位数等于i-1的二进制位数+1,又i-1是偶数,所以奇数i的二进制1的位数等于i/2中二进制1的位数+1.
所以上面的这些可以很简单的表达成answer[i] = answer[i >> 1] + (i & 1)
。
Python代码如下:
class Solution(object):
def countBits(self, num):
"""
:type num: int
:rtype: List[int]
"""
res = [0] * (num + 1)
for i in range(1, num + 1):
res[i] = res[i / 2] + i % 2
return res
Java代码如下:
public class Solution {
public int[] countBits(int num) {
int[] answer = new int[num+1];
answer[0] = 0;
for(int i = 1; i < answer.length; i++){
answer[i] = answer[i >> 1] + (i & 1);
}
return answer;
}
}
C++代码如下:
class Solution {
public:
vector<int> countBits(int num) {
vector<int> res(num + 1, 0);
for (int i = 1; i <= num; i ++) {
res[i] = res[i / 2] + i % 2;
}
return res;
}
};
日期
2017 年 4 月 25 日
2018 年 12 月 4 日 —— 周二啦!
【LeetCode】338. Counting Bits 解题报告(Python & Java & C++)的更多相关文章
- LN : leetcode 338 Counting Bits
lc 338 Counting Bits 338 Counting Bits Given a non negative integer number num. For every numbers i ...
- 【LeetCode】191. Number of 1 Bits 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 右移32次 计算末尾的1的个数 转成二进制统计1的个 ...
- Java [Leetcode 338]Counting Bits
题目描述: Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculat ...
- leetcode 338. Counting Bits,剑指offer二进制中1的个数
leetcode是求当前所有数的二进制中1的个数,剑指offer上是求某一个数二进制中1的个数 https://www.cnblogs.com/grandyang/p/5294255.html 第三种 ...
- Leetcode 338. Counting Bits
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- 【剑指Offer】不用加减乘除做加法 解题报告(Java)
[剑指Offer]不用加减乘除做加法 解题报告(Java) 标签(空格分隔): 剑指Offer 题目地址:https://www.nowcoder.com/ta/coding-interviews 题 ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
随机推荐
- jumpserver——脚本安装
CentOS Linux release 7.7.1908 (Core) 3.10.0-1062.4.1.el7.x86_64 Initialize(){ yum update -y systemct ...
- 59. Divide Two Integers
Divide Two Integers My Submissions QuestionEditorial Solution Total Accepted: 66073 Total Submission ...
- 二叉树——根据遍历结果,画出对应的二叉树 转载至:http://canlynet.blog.163.com/blog/static/255013652009112602449178/
这道题目很经典,具体如下: 已知遍历结果如下,试画出对应的二叉树: 前序:A B C E H F I J D G K 中序:A H E C I F J B D K G 解题要点: 1.前序.中序.后序 ...
- mysql-计算排名
mysql计算排名,获取行号rowno 学生成绩表数据 SELECT * FROM table_score ORDER BY score DESC; 获取某个学生成绩排名并计算该学生和上一名学生成绩差 ...
- 如何利用官方SDK文件来辅助开发
如何利用官方SDK文件来辅助开发 1.首先要先知道什么是SDK? SDK或者SDK包指的是,半导体厂商针对自己研发的芯片,同步推出的一个软件开发工具包. 它可以简单的为某个程序设计语言提供应用程序接口 ...
- C#时间选择
<script type="text/javascript" src="http://www.shicishu.com/down/WdatePicker.js&qu ...
- tensorboard No dashboards are active for the current data set.
修改一下启动命令时的路径 位置示例: 命令为 E:\PYTHON_PROJECT\testTF\inceptionV1_net\log>tensorboard --logdir=TEC4FN ...
- A Child's History of England.33
To strengthen his power, the King with great ceremony betrothed his eldest daughter Matilda, then a ...
- Flink基础
一.抽象层次 Flink提供不同级别的抽象来开发流/批处理应用程序. 最低级抽象只提供有状态流.它 通过Process Function嵌入到DataStream API中.它允许用户自由处理来自 ...
- 【leetcode】721. Accounts Merge(账户合并)
Given a list of accounts where each element accounts[i] is a list of strings, where the first elemen ...