LeetCode——Counting Bits
Question
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.
Credits:
Special thanks to @ syedee for adding this problem and creating all test cases.
Solution
动态规划,这里要应用求一个整数,对应二进制数中1的个数的方法,i & (i - 1),也就是将i的最右边的1置为0.
那么每个整数中1的个数,就等于不包含最右边1以后1的个数再加1。 ret[i] = ret[i & (i - 1)] + 1.
Code
class Solution {
public:
vector<int> countBits(int num) {
vector<int> table(num + 1, 0);
for (int i = 1; i <= num; i++) {
table[i] = table[i & (i - 1)] + 1;
}
return table;
}
};
LeetCode——Counting Bits的更多相关文章
- LeetCode Counting Bits
原题链接在这里:https://leetcode.com/problems/counting-bits/ 题目: Given a non negative integer number num. Fo ...
- [LeetCode] Counting Bits 计数位
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the ...
- Leetcode之动态规划(DP)专题-338. 比特位计数(Counting Bits)
Leetcode之动态规划(DP)专题-338. 比特位计数(Counting Bits) 给定一个非负整数 num.对于 0 ≤ i ≤ num 范围中的每个数字 i ,计算其二进制数中的 1 的数 ...
- 【LeetCode】338. Counting Bits (2 solutions)
Counting Bits Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num ...
- LN : leetcode 338 Counting Bits
lc 338 Counting Bits 338 Counting Bits Given a non negative integer number num. For every numbers i ...
- 2016.5.16——leetcode:Reverse Bits(超详细讲解)
leetcode:Reverse Bits 本题目收获 移位(<< >>), 或(|),与(&)计算的妙用 题目: Reverse bits of a given 3 ...
- Week 8 - 338.Counting Bits & 413. Arithmetic Slices
338.Counting Bits - Medium Given a non negative integer number num. For every numbers i in the range ...
- leetcode 上的Counting Bits 总结
最近准备刷 leetcode 做到了一个关于位运算的题记下方法 int cunt = 0; while(temp) { temp = temp&(temp - 1); //把二进制最左边那 ...
- leetcode 338. Counting Bits,剑指offer二进制中1的个数
leetcode是求当前所有数的二进制中1的个数,剑指offer上是求某一个数二进制中1的个数 https://www.cnblogs.com/grandyang/p/5294255.html 第三种 ...
随机推荐
- 并发编程 - 线程 - 1.互斥锁/2.GIL解释器锁/3.死锁与递归锁/4.信号量/5.Event事件/6.定时器
1.互斥锁: 原理:将并行变成串行 精髓:局部串行,只针对共享数据修改 保护不同的数据就应该用不用的锁 from threading import Thread, Lock import time n ...
- Aggregated Counting-----hdu5439(2015 长春网络赛 找规律)
#include<stdio.h> #include<string.h> #include<iostream> #include<math.h> #in ...
- Incorrect string value: '\xF0\x9F\x98\x84\xF0\x9F 表情插入mysql 报错
导致报错的问题是 emoji表情是4位 mysql 5.5.3版本以下数据库(utf8格式为3位),不支持.需要更新mysql5.5.3及以上的版本数据库并设置默认或者表或者字段的格式为 utf8mb ...
- 19.Delete Documents-官方文档摘录
1 插入例子 db.inventory.insertMany( [ { item: "journal", qty: 25, size: { h: 14, w: 21, uom: & ...
- VS中的配置管理器
一. 活动解决方案配置 有Debug和Release两个基本选项. Debug:称为 调试版本,它包含调试信息,且不做任何优化,便于程序员调试: Release:称为 发布版本,它往往是进行 ...
- python学习笔记(九)函数返回多个值,列表生成式,循环多个变量,入参格式声明
一.函数返回多个值 1.函数如果返回多个值的话,它会把这几个值放到一个元组里面2.函数如果返回多个值的话,也可以用多个变量来接收 def say(): num1 = num2 = num3 = ret ...
- windows平台tensorboard的配置及使用
由于官网和其他教程里面都是以Linux为平台演示tensorboard使用的,而在Windows上与Linux上会有一些差别,因此我将学习的过程记录下来与大家分享(基于tensorflow1.2.1版 ...
- python16_day22【cmdb注释】
https://github.com/willianflasky/growup/tree/master/s16/day23/learnMadKing
- Delphi APP 開發入門(四)簡易手電筒
Delphi APP 開發入門(四)簡易手電筒 分享: Share on facebookShare on twitterShare on google_plusone_share 閲讀次數:32 ...
- C#中获取音频文件时长
1.在项目中添加引入:COM组件的Microsoft Shell Controls And Automation 2.在引用中找到Shell32,点击右键,在属性中将“嵌入互操作类型”的值改为“fal ...