mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-09 15:59:06 +01:00
problems(java): add “191. Number of 1 Bits”
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
62436de0d0
commit
8f34be49bb
1 changed files with 13 additions and 0 deletions
13
problems/java/number-of-1-bits.java
Normal file
13
problems/java/number-of-1-bits.java
Normal file
|
@ -0,0 +1,13 @@
|
|||
public class Solution {
|
||||
public int hammingWeight(int n) {
|
||||
int bits = 0;
|
||||
|
||||
for (int i = 0; i < 32; ++i) {
|
||||
if ((n & (1 << i)) != 0) {
|
||||
++bits;
|
||||
}
|
||||
}
|
||||
|
||||
return bits;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue