标签:

位运算

描述:

Count how many 1 in binary representation of a 32-bit integer.

解题思路:

统计一个int型的数的二进制表现形式中1的个数
1.与check power of 2中的解题形式非常相似,同样利用num&(num-1) 的结果来检查num中二进制形式上1的个数,区别在于Check Power of 2 是来检查是否存在1,而这一题主要是检查有几个1。
2. 此题可以利用 num = num&(num-1) 每次进行与运算可以消除一个1,在num等于0之前就可以计算出二进制序列中存在几个1了。

参考代码:

http://www.jiuzhang.com/solutions/count-1-in-binary/

LintCode刷题笔记-- Count1 binary的更多相关文章

  1. lintcode刷题笔记(一)

    最近开始刷lintcode,记录下自己的答案,数字即为lintcode题目号,语言为python3,坚持日拱一卒吧... (一). 回文字符窜问题(Palindrome problem) 627. L ...

  2. 【leetcode刷题笔记】Binary Tree Inorder Traversal

    Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...

  3. 【leetcode刷题笔记】Binary Tree Preorder Traversal

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  4. LintCode刷题笔记-- Maximal Square

    标签:动态规划 题目描述: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing a ...

  5. LintCode刷题笔记-- LongestCommonSquence

    标签:动态规划 题目描述: Given two strings, find the longest common subsequence (LCS). Your code should return ...

  6. 【leetcode刷题笔记】Binary Tree Level Order Traversal(JAVA)

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  7. 【leetcode刷题笔记】Binary Tree Level Order Traversal II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  8. LintCode刷题笔记-- PaintHouse 1&2

    标签: 动态规划 题目描述: There are a row of n houses, each house can be painted with one of the k colors. The ...

  9. LintCode刷题笔记-- Maximum Product Subarray

    标签: 动态规划 描述: Find the contiguous subarray within an array (containing at least one number) which has ...

随机推荐

  1. php基础学习过程

    1.基础知识 a.注释: <?php // 这是单行注释 # 这也是单行注释 /* 这是多行注释块 它横跨了 多行 */ ?> b.大小写敏感: 在 PHP 中,所有用户定义的函数.类和关 ...

  2. [转]10 Tips for Learning a New Technology

    We live in a very exciting time. Never before has education been so cheaply available to the masses ...

  3. vim中利用swp文件进行恢复

    经常电脑因为没电或者强行关闭vim,会导致原文件没有保存, 这种情况下vim会自动保存一个.swp文件,需要恢复时, 使用vim -r filename 期中-r意思为recovery 恢复之后最好删 ...

  4. 详解如何挑战4秒内百万级数据导入SQL Server(转)

      对于大数据量的导入,是DBA们经常会碰到的问题,在这里我们讨论的是SQL Server环境下百万级数据量的导入,希望对大家有所帮助.51CTO编辑向您推荐<SQL Server入门到精通&g ...

  5. 记一次server 2008 R2的按装过程

    项目上一直在用的dell务器在去年年底突然出现系统过期,导致c盘的东西全部丢失.我们用激活工具激活,还是没能找回丢失的东西. 为了装这个系统,跟同事一起折腾了好些次,最后发现安装服务器的时候有个磁盘阵 ...

  6. Java面试总结-基础篇1

    java多线程-- 自旋锁,偏向锁 好处:可以举Servlet和CGI的对比用户线程和守护线程的区别:用户线程结束后JVM会退出,然后守护线程才会终止(比如垃圾回收线程),如何在java中创建守护线程 ...

  7. 通过三个DEMO学会SignalR的三种实现方式 转载https://www.cnblogs.com/zuowj/p/5674615.html

    一.理解SignalR ASP .NET SignalR 是一个ASP .NET 下的类库,可以在ASP .NET 的Web项目中实现实时通信(即:客户端(Web页面)和服务器端可以互相实时的通知消息 ...

  8. openCV 矩阵(图像)操作函数

    有很多函数有mask,代表掩码,如果某位mask是0,那么对应的src的那一位就不计算,mask要和矩阵/ROI/的大小相等.大多数函数支持ROI,如果图像ROI被设置,那么只处理ROI部分 少部分函 ...

  9. request.getSession().getServletContext().getRealPath("upload/" ); 获取不到 tomcat 服务器目录

    上传一个文件,找不到该文件的位置 设置上传的文件是在项目中的话 可以通过查找项目的路径锁定上传的文件路径 解决步骤: 可以通过jsp页面 打印获取项目的物理路径 控制台即可输出项目路径 这只是找了了上 ...

  10. JavaScript的基础应用

    <!DOCTYPE html> <!--JavaScript基础1--> <html lang="en"> <head> <m ...