LeetCode 第 191 題 (Number of 1 Bits)
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 return 3.
這道題也非常簡單, 用個迴圈語句把 32 位都測一遍就行了。
class Solution {
public:
int hammingWeight(uint32_t n) {
int count = 0;
uint32_t test_bit = 1;
for(int i = 0; i < 32; i++)
{
if(test_bit & n) count ++;
test_bit = test_bit << 1;
}
return count;
}
};
相關文章
- [LeetCode] 191. Number of 1 BitsLeetCode
- leetcode刷題--Number of 1 BitsLeetCode
- 【LeetCode從零單排】No 191.Number of 1 Bits(考察位運算)LeetCode
- LeetCode 第 190 題 (Reverse Bits)LeetCode
- LeetCode 第 338 題 (Counting Bits)LeetCode
- LeetCode 第 9 題(Palindrome Number)LeetCode
- LeetCode 第 65 題(Valid Number)LeetCode
- leetcode刷題--Reverse BitsLeetCode
- LeetCode每日一題: 位1的個數(No.191)LeetCode每日一題
- Leetcode第1~10題LeetCode
- leetcode 191 位1的個數LeetCode
- 【leetcode.191】位1的個數LeetCode
- LeetCode-Reverse BitsLeetCode
- LeetCode-Count BitsLeetCode
- leetcode刷題--Happy NumberLeetCodeAPP
- 【LeetCode】Counting Bits(338)LeetCode
- LeetCode 338 Counting BitsLeetCode
- leetcode刷題--Excel Sheet Column NumberLeetCodeExcel
- LeetCode 題解(252) : Find the Duplicate NumberLeetCode
- leetcode [python] 【338】Counting BitsLeetCodePython
- Leetcode Number of islandsLeetCode
- LeetCode:Largest NumberLeetCode
- LeetCode - 解題筆記 - 8 - Palindrome NumberLeetCode筆記
- [CareerCup] 5.5 Number of Converted Bits 轉換數字所需的位數
- 338. Counting Bits--LeetCode RecordLeetCode
- [LeetCode] Third Maximum NumberLeetCode
- [LeetCode] Find the Duplicate NumberLeetCode
- LeetCode-Strobogrammatic NumberLeetCode
- LeetCode-Largest NumberLeetCode
- LeetCode-Number of IslandsLeetCode
- Leetcode Valid NumberLeetCode
- leetcode Palindrome NumberLeetCode
- Leetcode Single NumberLeetCode
- LeetCode題解第122題LeetCode
- LeetCode刷題之第701題LeetCode
- OS kernel Bits 32/64 bits?
- LeetCode 第 69 題 (Sqrt(x))LeetCode
- LeetCode 第 7 題(Reverse Integer)LeetCode