题目描述:

Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).

For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should return 3.

解题思路:

简单的除就可以了。

class Solution:
# @param n, an integer
# @return an integer
def hammingWeight(self, n):
res = 0
while n != 0:
if n % 2 != 0:
res += 1
n /=2
return res

【leetcode】Number of 1 Bits的更多相关文章

  1. 【leetcode】Number of 1 Bits (easy)

    做太多遍了,秒杀. class Solution { public: int hammingWeight(uint32_t n) { ; ), num++); return num; } };

  2. 【LeetCode】190 & 191 - Reverse Bits & Number of 1 Bits

    190 - Reverse Bits Reverse bits of a given 32 bits unsigned integer. For example, given input 432615 ...

  3. 【leetcode】Number of Islands(middle)

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  4. 【LeetCode】Number of Islands

    Number of Islands 问题描写叙述 Given a 2d grid map of '1's (land) and '0's (water), count the number of is ...

  5. 【LeetCode191】Number of 1 Bits★

    1.题目 2.思路 方法一:常规方法. 方法二:给面试官惊喜的解法. 3.java代码 方法一代码: public class Solution { // you need to treat n as ...

  6. 【LeetCode】位运算 bit manipulation(共32题)

    [78]Subsets 给了一个 distinct 的数组,返回它所有的子集. Example: Input: nums = [,,] Output: [ [], [], [], [,,], [,], ...

  7. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  8. 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)

    [LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  9. 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)

    [LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

随机推荐

  1. mysql 关系表 分组读取的方法

    关系表 是一个一对多的表 我们用的时候往往希望得到 array( a=>array(1,2,3,4....), b=>array(3,4,5,6,7...) ) 这样的数组 所以我们可以使 ...

  2. Linux下数据恢复软件extundelete

    extundelete软件专门解决意外删除事件的,有时候不小心 rm -rf * 就可能毁掉有用的数据.因此就出现了这个恢复工具,但这个工具也不是万能的,删除数据后一定要停止所以的写操作.以免Inod ...

  3. centos7安装svn1.8.16

    svn下载地址:http://subversion.apache.org/download/ svn要依赖一些包,可以提前装好 yum -y install apr-util apr-util-dev ...

  4. .Net Core 杂记

    在学习.net core的路上,遇到很多坑,慢慢了解了.net core设计理念和设计思想(纯属跟人理解). 再此整理了之前写的一些学习笔记,后续也会把新的学习新的加上. 1..net core 跨平 ...

  5. SQL Server群集如何在线检测

    SQL Server群集知识介绍 Windows群集安装 基于iSCSI的SQL Server 2012群集测试 前言 群集的检测是调用dll资源,例如对于共享存储,ip,网络名称与DTC 这类Win ...

  6. 他山之石——vs2013 安装与部署及程序打包

    C#打包需要这个:InstallShield 2013 Limited Edition for Visual Studio  .下载地址: InstallShield 2013 Limited Edi ...

  7. rpc-1-OSI模型

    rpc-1-OSI模型 第一部分,网络7层协议 1. OSI模型: 开放通信系统互联网参考模型,是国际标准化组织(ISO),提出的一个,试图使各种计算机在世界范围内互连为网络的模式.(遵循这个模式,计 ...

  8. runtime第三部分方法和消息

    接上一篇http://www.cnblogs.com/ddavidXu/p/5924049.html 转载来源http://www.jianshu.com/p/6b905584f536 http:// ...

  9. JAVA 各种数值类型最大值和最小值 Int, short, char, long, float,&nbs

    转载地址:http://blog.sina.com.cn/s/blog_5eab3d430101fdv6.html 代码片段: fmax = Float.MAX_VALUE; fmin = Float ...

  10. bzoj 4695: 最假女选手

    ……一道丧病线段树膜板题…… 被常数卡的死去活来……QAQ 学到了些奇技淫巧:把取min标记 和 区间最小值 合并 可以快很多…… #include <bits/stdc++.h> #de ...