[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 return 3.

很简单的一道位操作Bit Operation的题,最近新出的三道题都没有啥难度啊,这样会误导新人的,做了这三道得出个LeetCode没啥难度的结论,其实里面好题真的不少,难题也很多,经典题也多,反正就是赞赞赞,32个赞。

class Solution {
public:
    int hammingWeight(uint32_t n) {
        int res = 0;
        for (int i = 0; i < 32; ++i) {
            res += (n & 1);
            n = n >> 1;
        }
        return res;
    }
};
时间: 2024-10-07 11:30:38

[LeetCode] Number of 1 Bits 位1的个数的相关文章

[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

LeetCode Number of 1 Bits 计算1的个数

题意:提供一个无符号32位整型uint32_t变量,返回其二进制形式的1的个数. 思路:取出一位,就右移1位,挤掉它,循环32次,逐个判断.没难度就不解释了,可能有更好解法,等待第2次思考. 1 class Solution { 2 public: 3 int hammingWeight(uint32_t n) { 4 if(!n) return 0; 5 if(n==1) return 1; 6 7 uint32_t MASK = 1, temp=n; 8 int i, cnt = 0; 9

[LeetCode] Number of Digit One 数字1的个数

Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n. For example: Given n = 13, Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13. Hint: Beware of overflow.

2016.5.16——leetcode:Number of 1 Bits ,

leetcode:Number of 1 Bits 1.Number of 1 Bits 本题收获: 1.Hamming weight:即二进制中1的个数 2.n &= (n-1)[n = n & (n-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 exam

leetcode number of 1 bits python

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] 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]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] 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

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