-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
82 lines (76 loc) · 1.99 KB
/
main.cpp
File metadata and controls
82 lines (76 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Source: https://leetcode.com/problems/number-of-1-bits
// Title: Number of 1 Bits
// Difficulty: Easy
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Given a positive integer `n`, write a function that returns the number of **set bits** in its binary representation (also known as the **Hamming weight** https://en.wikipedia.org/wiki/Hamming_weight).
//
// A **set bit** refers to a bit in the binary representation of a number that has a value of 1.
//
// **Example 1:**
//
// ```
// Input: n = 11
// Output: 3
// Explanation:
// The input binary string **1011** has a total of three set bits.
// ```
//
// **Example 2:**
//
// ```
// Input: n = 128
// Output: 1
// Explanation:
// The input binary string **10000000** has a total of one set bit.
// ```
//
// **Example 3:**
//
// ```
// Input: n = 2147483645
// Output: 30
// Explanation:
// The input binary string **1111111111111111111111111111101** has a total of thirty set bits.
// ```
//
// **Constraints:**
//
// - `1 <= n <= 2^31 - 1`
//
// **Follow up:** If this function is called many times, how would you optimize it?
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <bit>
#include <cstdint>
using namespace std;
// Shift Loop
class Solution {
public:
int hammingWeight(int n) {
auto ans = 0;
for (; n > 0; n >>= 1) {
ans += n % 2;
}
return ans;
}
};
// Bit Mask
class Solution2 {
public:
int hammingWeight(uint32_t n) {
n = ((0xAAAAAAAAu & n) >> 1) + (0x55555555u & n);
n = ((0xCCCCCCCCu & n) >> 2) + (0x33333333u & n);
n = ((0xF0F0F0F0u & n) >> 4) + (0x0F0F0F0Fu & n);
n = ((0xFF00FF00u & n) >> 8) + (0x00FF00FFu & n);
n = ((0xFFFF0000u & n) >> 16) + (0x0000FFFFu & n);
return n;
}
};
// Builtin
class Solution3 {
public:
int hammingWeight(uint32_t n) { //
return popcount(n);
}
};