[LeetCode] 191. Number of 1 Bits ☆(位 1 的个数)

描述

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

输入是一个无符号整数,返回其二进制表达式中数字位数为 ‘1’ 的个数。

解析

消除最后的1

观察一下 n 与 n-1 这两个数的二进制表示:对于 n-1 这个数的二进制来说,相对于 n 的二进制,它的最末位的一个 1 会变成 0,最末位一个 1 之后的 0 会全部变成 1,其它位相同不变。

比如 n = 8888,其二进制为 10001010111000

则 n - 1 = 8887 ,其二进制为 10001010110111

通过按位与操作后:n & (n-1) = 10001010110000

也就是说:通过 n&(n-1)这个操作,可以起到消除最后一个1的作用。

所以可以通过执行 n&(n-1) 操作来消除 n 末尾的 1 ,消除了多少次,就说明有多少个 1 。

简单遍历

每次与&1,如果为1,说明最后一位为1,再右移一位。

代码

public class Solution {
    // you need to treat n as an unsigned value
    public int hammingWeight(int n) {
        int cnt = 0;
        while(n != 0){
            cnt++;
            n = n & (n - 1);
        }
        return cnt;
    }
}
public class Solution {
    // you need to treat n as an unsigned value
    public int hammingWeight(int n) {
        int count = 0;
        while (n != 0) {
            count += (n & 1);
            n = n >>> 1;
        }
        return count;
    }
}

原文地址:https://www.cnblogs.com/fanguangdexiaoyuer/p/10696657.html

时间: 2024-10-31 13:50:30

[LeetCode] 191. Number of 1 Bits ☆(位 1 的个数)的相关文章

Leetcode 191 Number of 1 Bits 位运算

统计一个值的二进制1的个数,用与(&)和向左移位(<<) 编程之美中的2.1节有详细解答. 解法一: 1 class Solution { 2 public: 3 int hammingWeight(uint32_t n) { 4 int ans = 0; 5 for(;n;n = n>>1){ 6 ans += n&1; 7 } 8 return ans; 9 } 10 }; 解法二: 1 class Solution { 2 public: 3 int hamm

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-bit integer '11' has binary representation 00000000000000000000000000001011, so the function should retu

LeetCode 191 Number of 1 Bits(1 比特的数字们)

翻译 写一个函数获取一个无符号整型数,并且返回它的"1"比特的数目(也被叫做Hamming weight). 例如,一个32位整型数"11",转换成二进制是00000000000000000000000000001011,所以这个函数应该返回3. 原文 Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the

[LeetCode] 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-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should retu

Java for 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, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should retu

(easy)LeetCode 191.Number of 1 Bits

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-bit integer ’11' has binary representation 00000000000000000000000000001011, so the fun

LeetCode 191. Number of 1 Bits QuestionEditorial Solution

题意:给你一个整数,计算该整数的二进制形式里有多少个“1”.比如6(110),就有2个“1”. 一开始我就把数字n不断右移,然后判定最右位是否为1,是就cnt++,否则就继续右移直到n为0. 可是题目说了是无符号整数,所以给了2147483648,就WA了. 因为java里的int默认当做有符号数来操作的,而2147483648超过int的最大整数,所以在int里面其实是当做-1来计算的. 那么,不能再while里面判断n是否大于0,和使用位操作符>>.应该使用位操作符>>>

LeetCode:Number of 1 Bits - 整数的汉明重量

1.题目名称 Number of 1 Bits(整数的汉明重量) 2.题目地址 https://leetcode.com/problems/number-of-1-bits/ 3.题目内容 英文:Write a function that takes an unsigned integer and returns the number of '1' bits it has. 中文:写一个函数,输入一个无符号整数,返回其中值为1的比特位的个数(这个值也被称为数字汉明重量) 例如,32位整型数字11

338. Counting Bits &amp;&amp; 191. Number of 1 Bits

Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array. Example:For num = 5 you should return [0,1,1,2,1,2]. Follow up: It is very e