Leetcode:338. Bit位计数
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 @ syedeefor adding this problem and creating all test cases.
给定一个非负整数 num。 对于范围 0 ≤ i ≤ num 中的每个数字 i ,计算其二进制数中的1的数目并将它们作为数组返回。
示例:
比如给定 num = 5 ,应该返回 [0,1,1,2,1,2].
进阶:
- 给出时间复杂度为O(n * sizeof(integer)) 的解答非常容易。 但是你可以在线性时间O(n)内用一次遍历做到吗?
- 要求算法的空间复杂度为O(n)。
- 你能进一步完善解法吗? 在c ++或任何其他语言中不使用任何内置函数(如c++里的 __builtin_popcount)来执行此操作。
致谢:
特别感谢 @syedee添加此问题及所有测试用例。
本题,首先根据题目要求,我们不考虑c++里的 __builtin_popcount进行直接计算。
计算其二进制数中的1的数目,我们首先写出部分非负整数并转化为二进制形式,来发现其中的规律。
0 0000 0
1 0001 1
2 0010 1
3 0011 2
4 0100 1
5 0101 2
6 0110 2
7 0111 3
8 1000 1
9 1001 2
10 1010 2
11 1011 3
12 1100 2
13 1101 3
14 1110 3
15 1111 4
根据写出的数据,我们可以看到两个较为明显的规律,从而得出两个比较容易的方法
方法一:我们首先可以看到每个i值都是i&(i-1)对应的值加1,例:4的个数是4&3的值的个数再加1,关于与(&)操作,可以查询网上相关资料,代码如下:
class Solution {
public:
vector<int> countBits(int num) {
vector<int> res;
for(int i=;i<=num;++i)
{
res[i]=res[i&(i-)]+;
}
return res;
}
};
方法二:我们可以看出奇数的1的个数是该数除以2后得到的数的1的个数加1,例:3的对应的1的个数是3/2的个数+1即1+1=2,代码如下:
class Solution {
public:
vector<int> countBits(int num) {
vector<int> res{};
for (int i = ; i <= num; ++i) {
if (i % == ) res.push_back(res[i / ]);
else res.push_back(res[i / ] + );
}
return res;
}
};
Leetcode:338. Bit位计数的更多相关文章
- LeetCode 338. 比特位计数
338. 比特位计数 题目描述 给定一个非负整数 num.对于 0 ≤ i ≤ num 范围中的每个数字 i ,计算其二进制数中的 1 的数目并将它们作为数组返回. 示例 示例 1: 输入: 2 输出 ...
- Java实现 LeetCode 338 比特位计数
338. 比特位计数 给定一个非负整数 num.对于 0 ≤ i ≤ num 范围中的每个数字 i ,计算其二进制数中的 1 的数目并将它们作为数组返回. 示例 1: 输入: 2 输出: [0,1,1 ...
- Leetcode——338. 比特位计数
题目描述:题目链接 对于求解一个十进制数转化为二进制时里面1的个数,可以先看一下概况: 十进制数 二进制数 1的个数 1 1 1 2 10 1 3 11 2 4 100 1 5 101 2 ...
- Leetcode之动态规划(DP)专题-338. 比特位计数(Counting Bits)
Leetcode之动态规划(DP)专题-338. 比特位计数(Counting Bits) 给定一个非负整数 num.对于 0 ≤ i ≤ num 范围中的每个数字 i ,计算其二进制数中的 1 的数 ...
- leetcode TOP100 比特位计数
338. 比特位计数 题目描述: `给定一个非负整数 num.对于 0 ≤ i ≤ num 范围中的每个数字 i ,计算其二进制数中的 1 的数目并将它们作为数组返回. 示例 1: 输入: 2 输出: ...
- 338.比特位计数( Counting Bits)leetcode
附上:题目地址:https://leetcode-cn.com/problems/counting-bits/submissions/ 1:题目: 给定一个非负整数 num.对于 0 ≤ i ≤ nu ...
- 【Leetcode】338. Bit位计数
每次刷leetcode都有一种发现新大陆的感觉. 题目链接:https://leetcode-cn.com/problems/counting-bits/description/ 给定一个非负整数 n ...
- C#LeetCode刷题-位运算
位运算篇 # 题名 刷题 通过率 难度 78 子集 67.2% 中等 136 只出现一次的数字 C#LeetCode刷题之#136-只出现一次的数字(Single Number) 53.5% 简单 ...
- Leetcode题目338:比特位计数(中等)
题目描述: 给定一个非负整数 num.对于 0 ≤ i ≤ num 范围中的每个数字 i ,计算其二进制数中的 1 的数目并将它们作为数组返回. 示例 1: 输入: 2 输出: [0,1,1] 示例 ...
随机推荐
- php字符串与数组的特殊情况
来看一个有趣的实验 实验1 <?php $arr = array('a','b','c'); var_dump(isset($arr[1][0])); var_dump($arr[0][0]); ...
- 09-Python入门学习-函数基础与参数
一.函数基础 1.定义函数的三种形式 1.1 无参函数 def foo(): print('from foo') foo() 1.2 有参函数 def bar(x,y): print(x,y) bar ...
- C++的编译预处理
C++中,在编译器对源程序进行编译之前,首先要由预处理对程序文本进行预处理.预处理器提供了一组预编译处理指令和预处理操作符.预处理指令实际上不是C++语言的一部分,它只是用来扩充C++程序设计的环境. ...
- SQL Server查询重复数据
1.查询单列重复: select * from test where name in (select name from test group by name having count (name) ...
- vue项目中引入mint-ui的方式(全部引入与按需引入)
参考哦 https://blog.csdn.net/qq_36742720/article/details/83620584 https://jingyan.baidu.com/article/c1a ...
- Centos7 编译安装 Nginx PHP Mariadb Memcached 扩展 ZendOpcache扩展 (实测 笔记 Centos 7.3 + Openssl 1.1.0e + Mariadb 10.1.22 + Nginx 1.12.0 + PHP 7.1.4 + Laravel 5.4 )
环境: 系统硬件:vmware vsphere (CPU:2*4核,内存2G,双网卡) 系统版本:CentOS-7-x86_64-Minimal-1611.iso 安装步骤: 1.准备 1.0 查看硬 ...
- java中int和integer
- C语言面试题分类->链表
链表的创建,清空,插入,删除 typedef int (* __compfunc)(const void *, const void *); //Traverse list. Fast macro t ...
- Daily Pathtracer!安利下不错的Pathtracer学习资料
0x00 前言 最近看到了我司大网红aras-p(Aras Pranckevičius)的博客开了一个很有趣的新系列<Daily Pathtracer~>,来实现一个简单的ToyPathT ...
- [FPGA] 1、Artix-7 35T Arty FPGA 评估套件学习 + SiFive risc-v 指令集芯片验证
目录 1.简介 2.深入 3.DEMO 4.SiFive基于risc-v指令集的芯片验证 LINKS 时间 作者 版本 备注 2018-10-09 08:38 beautifulzzzz v1.0 到 ...