【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 ...
随机推荐
- 软件定义网络基础---SDN控制平面
一:SDN控制平面 一个或多个SDN控制器组成,是网络的大脑. 对底层网络交换设备进行集中管理,状态监测.转发决策以及处理和调 度数据平面的流量: 通过北向接口向上层应用开放多个层次的可编程能 ...
- 123457123456#0#-----com.tym.myNewShiZi45--前拼后广--识字tym
com.tym.myNewShiZi45--前拼后广--识字tym
- java 特殊字符处理
// 去除富文本中的html标签 // <p>段落替换为换行 content = content.replaceAll("<p .*?>", "\ ...
- 【翻译】Flink 异步I / O访问外部数据
本文来自官网翻译: Asynchronous I/O for External Data Access 需要异步I / O操作 先决条件 异步I / O API 超时处理 结果顺序 活动时间 容错保证 ...
- ubuntu18.04安装golang
首先我们通过如下命令安装golang: sudo add-apt-repository ppa:longsleep/golang-backports sudo apt-get update sudo ...
- 【原生JS插件】LoadingBar页面顶部加载进度条
先展示一下已经实现的效果: 预览地址:http://dtdxrk.github.io/js-plug/LoadingBar/index.html 看到手机上的浏览器内置了页面的加载进度条,想用在pc上 ...
- Cas(09)——通过Proxy访问其它Cas应用
通过Proxy访问其它Cas应用 目录 1.1 原理 1.2 配置 1.2.1 代理端 1.2.2 被代理端 1.3 请求示例 考虑这样一种场景:有两个应用App1 ...
- 关于工作中.net转java遇到的一个远程调用传递重复参的问题。
工作中遇到一个很奇怪的传参问题.之前.net使用的是一个List列表,列表中有几个重复的参数.列表中使用的model类是KeyValue. 我使用java模仿其写法,传递List和KeyValue.对 ...
- Mysql 定时备份(mysqldump)
#!/bin/bash today=`date +%Y-%m-%d` deleday=`date -d '7 day ago' +%Y-%m-%d` path=/home/data/mysqlback ...
- maven 打包成 .jar 文件执行:没有主清单属性错误
报错原因是pom.xml配置文件中没有指定main入口信息,在pom.xml文件中添加如下代码: <build> <plugins> <plugin> <gr ...