原题

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的更多相关文章

  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  ...

  2. 【LeetCode】338. Counting Bits 解题报告(Python & Java & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目描述 Given a non negati ...

  3. 【LeetCode】190. Reverse Bits 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 二进制字符串翻转 位运算 日期 题目地址:https://le ...

  4. 【LeetCode】190. Reverse Bits

    题目: Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented ...

  5. 【Leetcode】338. Bit位计数

    每次刷leetcode都有一种发现新大陆的感觉. 题目链接:https://leetcode-cn.com/problems/counting-bits/description/ 给定一个非负整数 n ...

  6. LN : leetcode 338 Counting Bits

    lc 338 Counting Bits 338 Counting Bits Given a non negative integer number num. For every numbers i ...

  7. 【LeetCode】449. Serialize and Deserialize BST 解题报告(Python)

    [LeetCode]449. Serialize and Deserialize BST 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/pro ...

  8. 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)

    [LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...

  9. 【LeetCode】468. Validate IP Address 解题报告(Python)

    [LeetCode]468. Validate IP Address 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...

随机推荐

  1. k8s-基础环境配置(六)

    hostname配置1)修改主机名hostnamectl set-hostname xxx2)加入主机映射vim /etc/hosts……关闭selinuxsed -i '/^SELINUX/s/=. ...

  2. rf安装对应requests库的方法

    先要安装requests,再安装requestsLibrary pip install requests pip install robotframework-requests github地址 ht ...

  3. Flink 物理分区

    本文来自官网: https://ci.apache.org/projects/flink/flink-docs-release-1.9/dev/stream/operators/#physical-p ...

  4. MYSQL:基础——索引原理及慢查询优化

    MYSQL:基础——索引原理及慢查询优化 索引的数据结构 索引的数据结构是B+树.如下图所示,B+树的节点通常被表示为一组有序的数据项和子指针.图中第一个节点包含数据项3和5,包含三个指针,第一个指针 ...

  5. AMSR-E/AMSR-2数据介绍与下载

    1 AMSR-E数据介绍 The Advanced Microwave Scanning Radiometer for EOS (AMSR-E)是ADEOS-II 上的AMSR的改进版本, 是JAXA ...

  6. Win10利用CodeBlocks搭建Objective-C开发环境(一)

    为了学习ios开发,而手头没有苹果机,若在windows平台下学习objective-c编程.则需要安装OC开发环境, 经过在网上查阅各种相关资料,历经多次失败,终于安装并测试成功,特将安装过程和经验 ...

  7. 最新 安易迅java校招面经 (含整理过的面试题大全)

    从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿. 安易迅等10家互联网公司的校招Offer,因为某些自身原因最终选择了 安易迅.6.7月主要是做系统复习.项目复盘.Leet ...

  8. Django2.2_No installed app with label 'admin'

    版本:py37+django2.2 错误:项目启动时出现,No installed app with label 'admin' 原因:可以发现只要注释掉settings里面的数据库DATABASES ...

  9. [转帖]ORM框架的前世今生

    ORM框架的前世今生 https://www.cnblogs.com/7tiny/p/9551754.html 目录 一.ORM简介二.ORM的工作原理三.ORM的优缺点四.常见的ORM框架 一.OR ...

  10. hadoop常见问题收集

    hadoop 搭建 常用命令记录 快捷键安装在/user/local/bin目录下 nano 文件名 ctrl + k 剪切一行 ctrl + o 保存并重命名,不重命名直接enter ctrl + ...