lc 338 Counting Bits


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.

规律公式 Accepted

题干中提示可以利用时间复杂度仅为O(n)的方法来做,说明一定存在着某种规律,不需要把每个数进行除二取模累加这样死做。

经过分析,可以轻松地得知一个数i,它的二进制表示含有1的个数一定等于它除去倒数第一位之外剩余1的个数再加上最后一位是否为1。所以,可易得递推式:ans[i] = ans[i/2] + i%2,为了使得程序运行得更快,我们可以等价地用位运算去替代上式:ans[i] = ans[i >> 1] + (i & 1)

class Solution {
public:
vector<int> countBits(int num) {
vector<int> ans(num+1, 0);
for (int i = 1; i <= num; i++) ans[i] = ans[i >> 1] + (i & 1);
return ans;
}
};

LN : leetcode 338 Counting Bits的更多相关文章

  1. leetcode 338. Counting Bits,剑指offer二进制中1的个数

    leetcode是求当前所有数的二进制中1的个数,剑指offer上是求某一个数二进制中1的个数 https://www.cnblogs.com/grandyang/p/5294255.html 第三种 ...

  2. Java [Leetcode 338]Counting Bits

    题目描述: Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculat ...

  3. Leetcode 338. Counting Bits

    Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the ...

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

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

  6. 【LeetCode】Counting Bits(338)

    1. Description Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num ...

  7. 【leetcode】338 .Counting Bits

    原题 Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate t ...

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

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

  9. 338. Counting Bits

    https://leetcode.com/problems/counting-bits/ 给定一个非负数n,输出[0,n]区间内所有数的二进制形式中含1的个数 Example: For num = 5 ...

随机推荐

  1. P1219 八皇后 洛谷

    题目描述 检查一个如下的6 x 6的跳棋棋盘,有六个棋子被放置在棋盘上,使得每行.每列有且只有一个,每条对角线(包括两条主对角线的所有平行线)上至多有一个棋子. 上面的布局可以用序列2 4 6 1 3 ...

  2. cogs——7. 通信线路

    7. 通信线路 ★★   输入文件:mcst.in   输出文件:mcst.out   简单对比时间限制:1.5 s   内存限制:128 MB 问题描述 假设要在n个城市之间建立通信联络网,则连通n ...

  3. mybatis xml标签,批量插入

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-/ ...

  4. 在线文档分享工具 ShowDoc

    原文:https://www.oschina.net/p/showdoc https://www.showdoc.cc/

  5. [转载]【BlackHat 2017】美国黑客大会首日议题汇总,演讲PPT下载也在这里

    今年是 Black Hat 举办的第 20 个年头,高温酷暑也挡不住全世界黑客和安全人员奔赴拉斯维加斯的热情.毕竟这可是一年一度的盛大狂欢啊.今年的 BHUSA 从美国东部时间时间 7 月 22 日( ...

  6. 【Linux多线程】三个经典同步问题

    在了解了<同步与互斥的区别>之后,我们来看看几个经典的线程同步的例子.相信通过具体场景可以让我们学会分析和解决这类线程同步的问题,以便以后应用在实际的项目中. 一.生产者-消费者问题 问题 ...

  7. python爬虫实践

    模拟登陆与文件下载 爬取http://moodle.tipdm.com上面的视频并下载 模拟登陆 由于泰迪杯网站问题,测试之后发现无法用正常的账号密码登陆,这里会使用访客账号登陆. 我们先打开泰迪杯的 ...

  8. android学习一(了解android)

    声明:android学习文件中面的全部内容为都是整理来自第一行代码Android.在接下来的文章里我就不在进行反复的声明. 想看原版的能够买书看看.或者去作者的博客http://blog.csdn.n ...

  9. 【前端】jQuery的animate在火狐浏览器上不支持backgroundPosition的解决方法

    转载请注明出处:http://www.cnblogs.com/shamoyuu/p/4375678.html jQuery的animate是一个非常好用的东东,但某些动画效果支持得不够好,比如back ...

  10. 洛谷 P1236 算24点

    题目描述 几十年前全世界就流行一种数字游戏,至今仍有人乐此不疲.在中国我们把这种游戏称为"算24点".您作为游戏者将得到4个1~9之间的自然数作为操作数,而您的任务是对这4个操作数 ...