最近准备刷 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. Python之路第一课Day2--随堂笔记

    入门知识拾遗 一.bytes类型 bytes转二进制然后转回来 msg="张杨" print(msg) print(msg.encode("utf-8")) p ...

  2. PHP 创建缩略图

    一.成比例缩小图像 <?php /* 创建缩略图 */ $file = __DIR__.'/button.png'; $scale = 0.5; // 比例 $image = ImageCrea ...

  3. 用户点击确认登录,自动跳转下面地址得到code

    PHP获取微信openid 简单教程 WEB   2014年10月29日  10868浏览  6评论 获取code https://open.weixin.qq.com/connect/oauth2/ ...

  4. C# 程序实现功能目录

    1.根据年月日获取 星期几  http://www.cnblogs.com/liwp/p/5943226.html 2.json对象转换为泛型集合 http://www.cnblogs.com/liw ...

  5. JMeter--集合点设置(转)

    集合点:简单来理解一下,虽然我们的"性能测试"理解为"多用户并发测试",但真正的并发是不存在的,为了更真实的实现并发这感念,我们可以在需要压力的地方设置集合点, ...

  6. DP HDU1421

    搬寝室 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

  7. 关于sort排序

    JavaScript的数组排序函数 sort方法,默认是按照ASCII 字符顺序进行升序排列.arrayobj.sort(sortfunction);参数:sortFunction可选项.是用来确定元 ...

  8. 一步一步来做WebQQ机器人-(五)(发送消息||完结)

    × 本篇主要是: 发送QQ消息(to:好友,群),以及对小黄鸡抓包利用它的语言库 本文是WebQQ流程的最后一章 最后一章内容不多但我还是啰嗦,可能对大部分人都已知晓的流程方法我也会介绍一下 前面几个 ...

  9. android模拟器没有键盘的解决方法

    刚开始使用android模拟器的时候,发现自己创建的AVD启动后没有出现侧边的键盘,在网上搜索后,发现很多人都有这个问题,也有文章说直接使用PC上的键盘,因为有对应的快捷键.但是,没有键盘,始终不爽! ...

  10. Apache 配置多端口

    Apache 配置多端口,主要是以下步骤 1. 如果电脑是64位的,官网上下载WampServe,装的过程中如果出现msvcp110.dll丢失的话,解决办法如下: 1.1 首先是打开浏览器,在浏览器 ...