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 1:

Input: 2
Output: [0,1,1]

Example 2:

Input: 5
Output: [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.

想法:统计0-num之间的二进制表示中1的个数。用result[i]表示i的二进制表示中1的个数。利用动态规划,找出状态转移表达式result[i]=result[i&i-1]+1

class Solution {
public:
    vector<int> countBits(int num) {
        vector<);
        result[] = ;
         ;i <= num ; i++){
            result[i] = result[i & i-] + ;
        }
        return result;
    }
};

leetcode338—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. [Swift]LeetCode338. 比特位计数 | Counting Bits

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

  6. LeetCode Counting Bits

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

  7. Counting Bits

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

  8. LeetCode 第 338 题 (Counting Bits)

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

  9. [LeetCode] Counting Bits 计数位

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

随机推荐

  1. 不要62(hdu2089)

    不要62 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissi ...

  2. RestfulAPI超简单入门

    简单入门 REST -- REpresentational State Transfer,英语的直译就是"表现层状态转移" 是目前最流行的 API 设计规范,用于 Web 数据接口 ...

  3. java中递归实现复制多级文件夹

    常见的流的用法 递归实现复制多级文件夹 FileInputStream & FileOutputStream String content = null;//用来储存解码后的byte数组 in ...

  4. Android-事件分发(ViewGroup)

    http://blog.csdn.net/guolin_blog/article/details/9153747 http://blog.csdn.net/lmj623565791/article/d ...

  5. HDU1083(KB10-C 二分图最大匹配)

    Courses Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  6. POJ3616(KB12-R dp)

    Milking Time Time Limit: 1000MS    Memory Limit: 65536K Total Submissions: 9459  Accepted: 3935   De ...

  7. python内置函数每日一学 -- abs()

    abs(x) 官方文档解释: Return the absolute value of a number. The argument may be an integer or a floating p ...

  8. 网络编程协议(TCP和UDP协议,黏包问题)以及socketserver模块

    网络编程协议 1.osi七层模型 应用层  表示层  会话层  传输层  网络层  数据链路层  物理层 2.套接字 socket 有两类,一种基于文件类型,一种基于网络类型 3.Tcp和udp协议 ...

  9. LOJ#505. 「LibreOJ β Round」ZQC 的游戏(最大流)

    题意 题目链接 Sol 首先把第一个人能吃掉的食物删掉 然后对每个人预处理出能吃到的食物,直接限流跑最大流就行了 判断一下最后的最大流是否等于重量和 注意一个非常恶心的地方是需要把除1外所有人都吃不到 ...

  10. eclipse代码中每行的开始和结尾出现多余的特殊符号

    window -> preferences -> general -> editors -> text editors ->          show whitespa ...