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 第三种 ...
随机推荐
- 转!mysql备份与还原数据库
备份数据库:1) mysqldump -uroot -p db_name > 20181018_preprod_bak.sql2) 输入数据库密码 还原数据库:1. 系统命令行:mysqladm ...
- ovn-kubernetes安装指南
Master节点的安装 1.首先在master节点安装ovs和ovn: #!/bin/bash sudo apt-get install openvswitch-common openvswitch- ...
- MFC工具栏设计
工具栏中包含了一组用于执行命令的按钮,每个按钮都用一个图标来表示.当单击某个按钮时,会产生一个相应的消息,对这个消息的处理就是按钮的功能实现.将菜单中常用的功能放置在工具栏中,这样可以方便用户操作,省 ...
- 【IPC进程间通讯之中的一个】邮槽MailSlot
IPC进程间通信+邮槽MailSlot IPC(Inter-Process Communication.进程间通信). 现代计算机採用虚拟内存机制,为进程提 ...
- python学习笔记(八)python操作Excel
一.python操作excel,python操作excel使用xlrd.xlwt和xlutils模块,xlrd模块是读取excel的,xlwt模块是写excel的,xlutils是用来修改excel的 ...
- JAVA与ACM
这两天学了一下JAVA的语法,还没有学习后面的核心地方,突然间觉得JAVA这门语言很棒,我要在接下来的时间系统的学习一下.就这么愉快地决定了. Java对于大数计算这方面的优势很大.最重要的是代码量小 ...
- PAT 1151 LCA in a Binary Tree[难][二叉树]
1151 LCA in a Binary Tree (30 分) The lowest common ancestor (LCA) of two nodes U and V in a tree is ...
- 测试CDockablePane。 测试他的最基本的功能。
最近看到一句话: ××××××××××××××××××××××××××××××××××××××××××× CDockablePane是一个通用窗口容器,它主要有两个用途:在一个框架中悬浮或者停靠窗口. ...
- beego——session模块
session介绍 session是一个独立的模块,即你可以那这个模块应用于其它Go程序中. session模块是用来存储客户端用户,session目前只支持cookie方式的请求,如果客户端不支持c ...
- Embedding SQLite in a c programm
Embedding SQLite in a c programm The following program demonstrates how to embed SQLite into ...