【leetcode】338 .Counting Bits
原题
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.
解析
给一个数字,计算从0到该数字的二进制1的个数
不允许使用内置函数
思路
我的解法。。使用了内置函数
最优解:因为*2可以表示为<<2,即一个数的两倍和它的一半的1的个数和这个数相同;也即,若一个数除以2可以除尽,则1的个数就是n/2的1的个数,若除不尽,则为n/2+1
我的解法
public int[] countBits(int num) {
int[] result = new int[num + 1];
for (int i = 0; i <= num; i++) {
result[i] = Integer.bitCount(i);
}
return result;
}
最优解
public int[] countBitsOptimize(int num) {
int[] result = new int[num + 1];
for (int i = 1; i <= num; i++) {
result[i] = result[i >> 1] + (i & 1);
}
return result;
}
【leetcode】338 .Counting Bits的更多相关文章
- 【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 ...
- 【LeetCode】338. Counting Bits 解题报告(Python & Java & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目描述 Given a non negati ...
- 【LeetCode】190. Reverse Bits 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 二进制字符串翻转 位运算 日期 题目地址:https://le ...
- 【LeetCode】190. Reverse Bits
题目: Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented ...
- 【Leetcode】338. Bit位计数
每次刷leetcode都有一种发现新大陆的感觉. 题目链接:https://leetcode-cn.com/problems/counting-bits/description/ 给定一个非负整数 n ...
- LN : leetcode 338 Counting Bits
lc 338 Counting Bits 338 Counting Bits Given a non negative integer number num. For every numbers i ...
- 【LeetCode】449. Serialize and Deserialize BST 解题报告(Python)
[LeetCode]449. Serialize and Deserialize BST 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/pro ...
- 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)
[LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...
- 【LeetCode】468. Validate IP Address 解题报告(Python)
[LeetCode]468. Validate IP Address 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
随机推荐
- Spring cloud微服务安全实战-3-7API安全机制之数据加密
这一节来聊一下密码的加密. 加密盐,为了避免两个相同的面加密出来的密文是一样的,每个人的盐不一样, 首先引入工具包,lambdaworks <!-- https://mvnrepository. ...
- Swift4.0复习类
1.类的属性: 2.类的方法: 3.类作为引用类型: “Swift新增了一对操作符 === 与 !== 用于判定同一个类的两个对象引用是否指向同一对象实例.” 摘录来自: “大话Swift 4.0”. ...
- Win10安装DB2配置笔记
响应文件名:D:\data\DB2\PROD_EXPC.rsp 安装文件夹--目录:D:\Program Files\IBM\SQLLIB\ IBM SSH Server安装位置:D:\Program ...
- LODOP指定window默认打印机和临时默认打印机
通过以下语句,可指定windows默认打印机LODOP.SET_PRINT_MODE("WINDOW_DEFPRINTER",某打印机名或序号);这种默认打印机是指的windows ...
- [Google] Help employee find the nearest gbike
You are given a campus map with the Google buildings, roads and Google bikes. You have to help the e ...
- Docker 安装运行MySQL
1.镜像主页 https://hub.docker.com/_/mysql 2.拉取5.7版本 docker pull mysql:5.7 3.或者拉取最新8.x版本 docker pull mysq ...
- Linux DNS 服务器配置与管理
一.环境介绍: 运行软件:VMware Workstation Pro 14 系统环境:CentOS-7-x86_64-1810 二.操作配置: 1.基础知识简介 (1)域名空间 域和域名: DNS树 ...
- Lua 学习笔记 (1)
最简单的 lua脚本 , do print ("line:", indx) end 也可以写成 , do print("line:", indx) end lu ...
- [转帖]Dockerfile: ENTRYPOINT和CMD的区别
Dockerfile: ENTRYPOINT和CMD的区别 https://zhuanlan.zhihu.com/p/30555962 在我们查阅Dockerfile的官方文档时, 有可能发现一些命令 ...
- Spring Boot 入门(八):集成RabbitMQ消息队列
本片文章续<Spring Boot 入门(七):集成 swagger2>,关于RabbitMQ的介绍请参考<java基础(六):RabbitMQ 入门> 1.增加依赖 < ...