关闭

[LeetCode] 191. Number of 1 Bits

标签: leetcodelintcode面试bit九章
201人阅读 评论(0) 收藏 举报
分类:

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.

思路:n和(n-1)按位与可以去掉二进制相对最右边一位1

          经过count次n&(n-1)直到n变为0,count就是n的二进制中表示1的个数

public class Solution {
    
    public int hammingWeight(int n) {
        int count=0;
        while(n!=0){
            count++;
            n=n&(n-1);
        }
        return count;
    }
}


0
0
查看评论

LeetCode-Number of 1 Bits

问题描述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-b...
  • canglingye
  • canglingye
  • 2015-03-10 16:49
  • 6310

[LeetCode-191] Number of 1 Bits(判断一个整数中有多少个1)

方法一:【分析】判断一个整数最右边是不是1,我们可以把输入的整数和1相&,判断相&之后的结果,判断是不是为1,然后继续移位判断其它位 方法二:【分析】上述的方法只适用于正数,当输入的数为一个负数的时候,比如按照方法一分析得,0x8000-0000,当右移一位变成0x0400-0000...
  • xy010902100449
  • xy010902100449
  • 2015-09-14 20:42
  • 1051

LeetCode 191 -Number of 1 Bits ( JAVA )

Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight). For exampl...
  • UckyK
  • UckyK
  • 2016-04-11 12:50
  • 836

Leetcode_191_Number of 1 Bits

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/44486547 Write a function that takes an unsigned integer and returns the numbe...
  • pistolove
  • pistolove
  • 2015-03-20 08:11
  • 1316

191. Number of 1 Bits [easy] (Python)

题目链接https://leetcode.com/problems/number-of-1-bits/题目原文 Write a function that takes an unsigned integer and returns the number of ’1’ bits it has (...
  • coder_orz
  • coder_orz
  • 2016-05-05 15:13
  • 1554

leetcode-191 Number of 1 Bits 二进制数字中1的个数

问题描述: 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, t...
  • woliuyunyicai
  • woliuyunyicai
  • 2015-03-10 19:42
  • 2816

191.[Leetcode]Number of 1 Bits

我的算法://将转化好的二进制字符串存入s中,然后从s中判断有多少的1 // 从10进制到其他进制,都是对其求余数 class Solution { public:int hammingWeight(uint32_t n) { string s = ""; ...
  • qq_28057541
  • qq_28057541
  • 2016-05-02 19:30
  • 121

[Leetcode]191. Number of 1 Bits

Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight). For exampl...
  • sunhero2010
  • sunhero2010
  • 2016-07-08 15:26
  • 73

LeetCode *** 191. Number of 1 Bits

题目: 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...
  • treeshy
  • treeshy
  • 2016-04-06 23:30
  • 78

[leetcode]: 191. Number of 1 Bits

1.题目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...
  • u010529144
  • u010529144
  • 2017-05-21 20:12
  • 135
    个人资料
    • 访问:18419次
    • 积分:1123
    • 等级:
    • 排名:千里之外
    • 原创:99篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条