最近准备刷 leetcode  做到了一个关于位运算的题记下方法

int cunt = 0;

while(temp)

{

  temp = temp&(temp - 1);  //把二进制最左边那个1变为零

  count++;   //统计1的个数

}

同理把位二进制坐左边那个0变为1

就可以  temp = temp|(temp + 1)

leetcode 上的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. 【Leetcode 338】 Counting Bits

    问题描述:给出一个非负整数num,对[0, num]范围内每个数都计算它的二进制表示中1的个数 Example:For num = 5 you should return [0,1,1,2,1,2] ...

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

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

  4. LeetCode题解之Counting Bits

    1.题目描述 2.问题分析 利用bitset. 3 代码 vector<int> countBits(int num) { vector<int> v; ; i <= n ...

  5. 【leetcode】338 .Counting Bits

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

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

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

  7. LN : leetcode 338 Counting Bits

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

  8. LeetCode Number of 1 Bits

    原题链接在这里:https://leetcode.com/problems/number-of-1-bits/ 题目: Write a function that takes an unsigned ...

  9. 关于Leetcode上二叉树的算法总结

    二叉树,结构很简单,只是比单链表复杂了那么一丢丢而已.我们先来看看它们结点上的差异: /* 单链表的结构 */ struct SingleList{ int element; struct Singl ...

随机推荐

  1. likely && unlikely in GCC

    在linux内核源码或一些比较成熟的c语言架构源码中,我们常会见到类似下面的代码: if (unlikely(!packet)) { return res_failed; } // OR if (li ...

  2. Java 开发环境的搭建

      配置JDK安装路径 1——JAVA_HOME 2——CLASSPATH               配置类库文件位置(特别注意:路径前面的”. “ 代表当前路径,分号用来区分路径) 3——PATH ...

  3. 使用File类列出指定位置下的文件及目录信息

    public static void main(String [] args) { File f=new File("C:"); File [] fl=f.listFiles(); ...

  4. Slave作为其它Slave的Master时使用

    主从配置需要注意的点 (1)主从服务器操作系统版本和位数一致: (2) Master和Slave数据库的版本要一致: (3) Master和Slave数据库中的数据要一致: (4) Master开启二 ...

  5. 打包ipa出现问题记录

    1.打包ipa事,Archive出现问题 解决方法:Xcode->系统偏好设置->Accounts->选中正在使用的账户->view details->download ...

  6. 解决导入so库报错小结

    最近公司准备将重构后的项目上线,可是等到我接手的时候发现一个很纠结的问题:安卓5.0以上的手机(例如我现在在用的红米note3)运行重构后项目发生报错,提示缺少某so库. 而5.0以下的手机(我的小米 ...

  7. Unity字节序问题

    问题 Unity中有些配置信息并不想在发布之后给其他人看到,所以在打包的时候进行了简单的编码处理,然后保存为.bytes类型,读取的时候再进行解码处理.今天遇到的很奇葩的问题是: 如果bytes文件U ...

  8. Gerrit与Gitlab同步配置replication&其他配置

    一.Gerrit与Gitlab同步配置 当配置好gerrit环境后,还需要与现有gitlab库进行同步配置,否则会影响现有开发与打包流程. 1.安装gerrit replication插件 unzip ...

  9. Ionic + AngularJS

    Ionic Framework Ionic framework is the youngest in our top 5 stack, as the alpha was released in lat ...

  10. 【Git学习笔记】撤销修改

    工作区下的.git文件夹其实是Git的版本库,Git的版本库里存了很多东西,其中最重要的就是称为 stage 的暂存区,还有Git为我们自动创建的第一个分支 master ,以及指向master的一个 ...