题目:

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

实现:

 class Solution {
public:
vector<int> countBits(int num) {
vector<int> result;
for (int i = ; i <= num; i++)
{
int count = ;
int j = i;
while (j)
{
++count;
j = (j-) & j;
}
result.push_back(count);
}
return result;
}
};

Counting Bits(Difficulty: Medium)的更多相关文章

  1. Number of 1 Bits(Difficulty: Easy)

    题目: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also ...

  2. Longest Substring Without Repeating Characters(Difficulty: Medium)

    题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...

  3. ACM_Reverse Bits(反转二进制)

    Reverse Bits Time Limit: 2000/1000ms (Java/Others) Problem Description: Reverse bits of a given 32 b ...

  4. 54.Counting Bits( 计算1的个数)

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

  5. 【LeetCode】Counting Bits(338)

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

  6. HDU 3450 Counting Sequences(线段树)

    Counting Sequences Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/65536 K (Java/Other ...

  7. LeetCode算法题-Binary Number with Alternating Bits(Java实现)

    这是悦乐书的第292次更新,第310篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第160题(顺位题号是693).给定正整数,检查它是否具有交替位:即它的二进制数的任意两 ...

  8. LeetCode算法题-Number of 1 Bits(Java实现)

    这是悦乐书的第186次更新,第188篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第45题(顺位题号是191).编写一个带无符号整数的函数,并返回它所具有的"1 ...

  9. LeetCode算法题-Reverse Bits(Java实现)

    这是悦乐书的第185次更新,第187篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第44题(顺位题号是190).给定32位无符号整数,求它的反转位.例如: 输入:4326 ...

随机推荐

  1. jQuery正则的使用方法步骤详解

    本文主要和大家分享的就是jQuery学习中正则的使用,正则在jquery里面并没有比javascript多哪些知识,基本上是一样的,只是选择器更好了一点,一起来看看吧. 基础正则 1.正则表达式的创建 ...

  2. Ruby--学习记录(实时更新)

    变量的命名方式决定了变量的种类: 局部变量  以英文字母或者_开头: 全局变量  以$开头: 实例变量  以@开头: 类变量     以@@开头:

  3. Git常用命令速查表

  4. 【adb】adb基本命令总结

    adb常用基本命令如下: adb devices           列出你的devices aapt dump badging <file_path.apk>     查看包名 adb ...

  5. Myeclipse的使用

    一,错误解决 1, 现象:使用eclipse运行带有main函数的Java文件时,出现editor does not contain a main type的错误框 原因:原来这个class所在包没有 ...

  6. 媒体查询使用方法@media

    Media Queries能在不同的条件下使用不同的样式,使页面在不同在终端设备下达到不同的渲染效果.前面简单的介绍了Media Queries如何引用到项目中,但Media Queries有其自己的 ...

  7. 从下往上看--新皮层资料的读后感 第二部分:突触Synapses

    为进一步了解这种连接性产生的差异,在认识soma这个独立的逻辑单元后,进化过程设计了一整套系统设计来使用这个逻辑单元.为促成细胞体之间发生连接构成系统dendrite和axon之间的连接需要引入新的功 ...

  8. JAVA类与对象

    Employee类: public class EmployeeTest { public static void main(String[] args) { // fill the staff ar ...

  9. ueditor使用小结

    一.简介 ueditor是百度编辑器,官网地址:http://ueditor.baidu.com/website/ 完整的功能演示,可以参考:http://ueditor.baidu.com/webs ...

  10. EF中的Code First

     一些概念 Ÿ POCO POCO(Plain Old CLR Object)的概念是从java的POJO借用而来,而两者的含义是一致的,不同的仅仅是使用的语言不一样.所以POCO的解释就是“Plai ...