forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_762.java
37 lines (34 loc) · 914 Bytes
/
_762.java
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
package com.fishercoder.solutions;
public class _762 {
public static class Solution1 {
public int countPrimeSetBits(int L, int R) {
int count = 0;
for (int i = L; i <= R; i++) {
if (hasPrimeNumberSetBits(i)) {
count++;
}
}
return count;
}
private boolean hasPrimeNumberSetBits(int num) {
int k = getSetBits(num);
if (k <= 1) {
return false;
}
for (int i = 2; i * i <= k; i++) {
if (k % i == 0) {
return false;
}
}
return true;
}
private int getSetBits(int n) {
int bits = 0;
while (n != 0) {
bits++;
n &= (n - 1);
}
return bits;
}
}
}