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. 七牛云上传图片到cdn,cdn返回的是一个只有图片的url

    如:

  2. chassis & power

    机箱电源 ★ Main board ★ Voltage, connector ★ Hole ★ Ports ★ AT:12``*13.8`` or 12``*13``  30.5cm*33cm ★ B ...

  3. CentOS 7 防火墙开启了哪些服务和端口?

    过滤封包功能的 netfilter 已经内建在 CentOS 7 的内核,但是配置 netfilter 的界面程式 firewalld 却未必有安装,不论是否已经安装,都可以执行下面的安装指令: yu ...

  4. android:“新版飞机大战”源码开源啦!

    今天10.24,为了纪念程序猿的节日,把之前写过的一个"飞机大战"的一个源码开源了. 源码地址:https://github.com/nuptboyzhb/newplanegame ...

  5. Android上拉查看详情实现

    京东淘宝有那么一种效果就是,上拉能够查看宝贝的详情,这里我也实现了一个类似的效果,也能够移植到商业项目上:先看看简单的效果图 实现原理事实上是利用了ScrollView的滚动和view的touch事件 ...

  6. MySQL-数据表锁定

    MySQL允许客户端会话明确获取表锁,以防止其他会话在特定时间段内访问表.客户端会话只能为自己获取或释放表锁.它不能获取或释放其他会话的表锁. 创建一个数据表: CREATE DATABASE IF ...

  7. WWDC笔记:2013 Session 201 Building User Interfaces for iOS 7

    Text Dynamic Type Specifies fonts semantically Supports user text sizing Optimized for legibility Su ...

  8. (三)Java 开发环境配置

    window系统安装java 下载JDK   首先我们需要下载java开发工具包JDK,下载地址:http://www.oracle.com/technetwork/java/javase/downl ...

  9. 为JMenu中的JPopupMenu定制透明背景

    最近研究了很久这个问题,从LookAndFeel到继承JPopupMenu或者JMenu都搞不定. 其实替换背景的话,只要在JMenuUI中设置Opaque(false) 再将背景设置透明就可以看到P ...

  10. Android原生音量控制【转】

    本文转载自:http://blog.csdn.net/u013082948/article/details/65630085 本文主要涉及AudioService.还是基于5.1.1版本的代码. Au ...