【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 ...
随机推荐
- C语言中内存对齐与结构体
结构体 结构体是一种新的数据类型,对C语言的数据类型进行了极大的扩充. struct STU{ int age; char name[15]; }; struct STU a; //结构体实例 str ...
- 巩固javaweb第三天
巩固内容: HTML 标题 HTML 标题(Heading)是通过<h1> - <h6> 标签来定义的. HTML 段落 HTML 段落是通过标签 <p> 来定义的 ...
- academy
academy at/in school都行,academy一般用at. The word comes from the Academy in ancient Greece, which derive ...
- Java、Scala获取Class实例
Java获取Class实例的四种方式 package com.test; /** * @description: TODO * @author: HaoWu * @create: 2020/7/22 ...
- Sharding-JDBC 实现水平分库分表
1.需求分析
- 【STM32】晶振,主时钟,外设频率介绍
首先,我用的是STM32F407,下方所有图片都是出自这芯片的文档,如果型号和我不同,需要找到对应的芯片说明文档,也许会有出入 先看一张时钟图 这里会着重说明高速的部分,低速(不管内部还是外部)只给R ...
- Postman 中 Pre-request Script 常用 js 脚本
1. 生成一个MD5或SHA1加密的字符串str_md5,str_sha1 string1 = "123456"; var str_md5= CryptoJS.MD5(string ...
- Linux基础命令---nslookup查询域名工具
nslookup nslookup是一个查询DNS域名的工具,它有交互和非交互两种工作模式. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.Fedora. 1.语法 ...
- 记录一下使用MySQL的left join时,遇到的坑
# 现象 left join在我们使用mysql查询的过程中可谓非常常见,比如博客里一篇文章有多少条评论.商城里一个货物有多少评论.一条评论有多少个赞等等.但是由于对join.on.where等关键字 ...
- RTTI (Run-time type information) in C++
In C++, RTTI (Run-time type information) is available only for the classes which have at least one v ...