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

From:

1. Naive Solution

We can simply count bits for each number like the following:

 public class Solution {
public int[] countBits(int num) {
int[] result = new int[num + ]; for (int i = ; i <= num; i++) {
result[i] = countEach(i);
} return result;
} public int countEach(int num) {
int result = ; while (num != ) {
if ((num & ) == ) {
result++;
}
num = num >>> ;
} return result;
}
}

2. Improved Solution

For number 2(10), 4(100), 8(1000), 16(10000), ..., the number of 1's is 1. Any other number can be converted to be 2^m + x. For example, 9=8+1, 10=8+2. The number of 1's for any other number is 1 + # of 1's in x.

     public int[] countBits(int num) {
int[] result = new int[num + ]; int pow = ;
for (int i = ; i <= num; i++) {
if (i == pow) {
result[i] = ;
pow = pow << ;
} else {
int p = pow >> ;
result[i] = result[p] + result[i - p];
}
}
return result;
}

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. LN : leetcode 338 Counting Bits

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

  3. Leetcode之动态规划(DP)专题-338. 比特位计数(Counting Bits)

    Leetcode之动态规划(DP)专题-338. 比特位计数(Counting Bits) 给定一个非负整数 num.对于 0 ≤ i ≤ num 范围中的每个数字 i ,计算其二进制数中的 1 的数 ...

  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 Counting Bits

    原题链接在这里:https://leetcode.com/problems/counting-bits/ 题目: Given a non negative integer number num. Fo ...

  6. LeetCode 第 338 题 (Counting Bits)

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

  7. [LeetCode] Counting Bits 计数位

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

  8. 338. Counting Bits

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

  9. 【LeetCode】Counting Bits(338)

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

随机推荐

  1. javascript this 详解

    前言 Javascript是一门基于对象的动态语言,也就是说,所有东西都是对象,一个很典型的例子就是函数也被视为普通的对象.Javascript可以通过一定的设计模式来实现面向对象的编程,其中this ...

  2. 在 Visual Studio 2013 中创建 ASP.NET Web 项目(1):概述 - 创建 Web 应用程序项目

    注:本文是“在 Visual Studio 2013 中创建 ASP.NET Web 项目”专题的一部分,详情参见 专题导航 . 预备知识 本专题适用于 Visual Studio 2013 及以上版 ...

  3. JS模式:Mixin混合模式,=_=!就是常见的Object.create()或者_extend()

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. Ajax 的缺点

    1.ajax干掉了back按钮,即对浏览器后退机制的破坏.后退按钮是一个标准的web站点的重要功能,但是它没法和js进行很好的合作.这是ajax所带来的一个比较严重的问题,因为用户往往是希望能够通过后 ...

  5. PHP Simulation HTTP Request(undone)

    目录 . 引言 . file_get_contents版本 . Socket版本 . Curl版本 . Curl版本() . 模拟文件上传 0. 引言 本文总结了通过PHP代码方式模拟各种HTTP请求 ...

  6. vim下使用ctags+taglist

    好几年前用过,但是后来就生疏了,好其次都没法鼓起勇气再捡起来...今天不得不用,那既然捡起来了,就好好的记录一下. 简介及安装 ctags是一个应用程序,可以用它来生产当前目录下所有c文件中变量和函数 ...

  7. hihocoder 1181 欧拉路.二

    传送门:欧拉路·二 #1181 : 欧拉路·二 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在上一回中小Hi和小Ho控制着主角收集了分散在各个木桥上的道具,这些道具其 ...

  8. 在微信中实现app软件中账号注册的功能实现

    利用写好的接口url地址访问 输入手机号,接收手机验证码 <span class="accept" >点击获取验证码</span> $(".acc ...

  9. structs2标签简单实用,及自定义转换器示例代码

    一.在structs.xml中配置 <structs> <package name="tagp" namespace="/test" exte ...

  10. 转载大神的检测网站重定向的python脚本

    #!/usr/bin/env python #coding=utf8 import sys import requests def check_for_redirects(url): try: r = ...