最近准备刷 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. Android 之 ListView的学习

    ListView 是一个控件,一个在垂直滚动的列表中显示条目的一个控件,这些条目的内容来自于一个ListAdapter .EditText Button TextView ImageView Chec ...

  2. [转]RMAN检测数据库坏块

    backup validate check logical database; select * from v$database_block_corruption; RMAN> backup v ...

  3. go语言学习笔记

    Go语言学习基本类型Bool 取值范围:true,false (不可以用数字代替)Int/uint 根据平台可能为32或64位int8/uint8 长度:1字节 取值范围-128~127/0~255b ...

  4. JS问题汇总

    1.Q:$(this)在js中失效,无法获取当前元素 A​:function()在被调用时this是指向window的,如果要想指向被点击的元素,一般是将this作为参数传入,例如: <div ...

  5. GIT如何添加权限模块

    http://blog.chinaunix.net/uid-15174104-id-3843570.html

  6. js中网页高度与宽度那些事

    网页可见区域宽:document.body.clientWidth; 网页可见区域高:document.body.clientHeight; 网页可见区域宽:document.body.offsetW ...

  7. Execel(导出新方法):

    #region 新方法 //var sbHtml = new StringBuilder(); //sbHtml.Append("<table border='1' cellspaci ...

  8. Week 1:2015/4/27~2015/5/3

    Update everyday.(Last edit:4/30 01:00) Task 1:TPO X 2.5(finish 1,then finish 2 more) Task 2:TC Tarja ...

  9. sublime通用快捷键 汉化 安装 插件

    Ctrl+Alt+P     切换项目 1.Ctrl+Shift+P     打开Package Control     Ctrl + Shift + P ,输入View, 选择Toogle Tabs ...

  10. canvas简单图片处理(灰色处理)

    反色处理写的比较简单,灰色处理写了一些注释 <!DOCTYPE html> <html> <head> <meta charset="UTF-8&q ...