-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path762.java
More file actions
64 lines (57 loc) · 1.6 KB
/
762.java
File metadata and controls
64 lines (57 loc) · 1.6 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
__________________________________________________________________________________________________
sample 1 ms submission
class Solution {
public int countPrimeSetBits(int L, int R) {
int count = 0;
P[2] = true;
P[3] = true;
P[5] = true;
P[7] = true;
P[11] = true;
P[13] = true;
P[17] = true;
P[19] = true;
for (int i = L; i <= R; i++) {
if(isPrimeSet(i)) count++;
}
return count;
}
boolean[] P = new boolean[20];
boolean isPrimeSet(int x) {
int c = Integer.bitCount(x);
return P[c];
}
}
__________________________________________________________________________________________________
sample 31844 kb submission
class Solution {
public int countPrimeSetBits(int L, int R) {
int count = 0;
for (int i=L; i<=R; i++) {
if (isPrime(numberOfOnes(i))) {
count++;
}
}
return count;
}
public boolean isPrime(int num) {
if (num <= 1) return false;
int bound = (int) Math.sqrt(num);
// boolean prime = true;
for (int i=2; i<=bound; i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
public int numberOfOnes (int num) {
int count = 0;
while (num != 0) {
num = num & (num-1);
count++;
}
return count;
}
}
__________________________________________________________________________________________________