File tree 5 files changed +94
-0
lines changed
5 files changed +94
-0
lines changed Original file line number Diff line number Diff line change @@ -80,6 +80,7 @@ All solutions will be accepted!
80
80
| 268| [ Missing Number] ( https://leetcode-cn.com/problems/missing-number/description/ ) | [ java/py/js] ( ./algorithms/MissingNumber ) | Easy|
81
81
| 788| [ Rotated Digits] ( https://leetcode-cn.com/problems/rotated-digits/description/ ) | [ java/py/js] ( ./algorithms/RotatedDigits ) | Easy|
82
82
| 695| [ Max Area Of Island] ( https://leetcode-cn.com/problems/max-area-of-island/description/ ) | [ java/py/js] ( ./algorithms/MaxAreaOfIsland ) | Easy|
83
+ | 762| [ Prime Number Of Set Bits In Binary Representation] ( https://leetcode-cn.com/problems/prime-number-of-set-bits-in-binary-representation/description/ ) | [ java/py/js] ( ./algorithms/PrimeNumberOfSetBitsInBinaryRepresentation ) | Easy|
83
84
84
85
# Database
85
86
| #| Title| Solution| Difficulty|
Original file line number Diff line number Diff line change
1
+ # Prime Number Of Set Bits In Binary Representation
2
+ This problem is easy to solve
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int countPrimeSetBits (int L , int R ) {
3
+ int count = 0 ;
4
+
5
+ for (int i = L ; i <= R ; i ++) {
6
+ int oneCount = 0 , x = i ;
7
+
8
+ while (x > 1 ) {
9
+ x &= x - 1 ;
10
+ oneCount ++;
11
+ }
12
+
13
+ if (oneCount > 1 ) {
14
+ x = oneCount / 2 ;
15
+ boolean isPrime = true ;
16
+ while (x > 1 ) {
17
+ if (oneCount % x == 0 ) {
18
+ isPrime = false ;
19
+ break ;
20
+ } else {
21
+ x --;
22
+ }
23
+ }
24
+ if (isPrime ) count ++;
25
+ }
26
+ }
27
+
28
+ return count ;
29
+ }
30
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number } L
3
+ * @param {number } R
4
+ * @return {number }
5
+ */
6
+ var countPrimeSetBits = function ( L , R ) {
7
+ let count = 0
8
+
9
+ for ( let i = L ; i <= R ; i ++ ) {
10
+ let oneCount = 0 , x = i
11
+ while ( x !== 0 ) {
12
+ x &= x - 1
13
+ oneCount ++
14
+ }
15
+
16
+ if ( oneCount !== 1 ) {
17
+ let x = parseInt ( oneCount / 2 ) ,
18
+ isPrime = true
19
+ while ( x > 1 ) {
20
+ if ( oneCount % x === 0 ) {
21
+ isPrime = false
22
+ break
23
+ } else {
24
+ x --
25
+ }
26
+ }
27
+ if ( isPrime ) count ++
28
+ }
29
+ }
30
+
31
+ return count
32
+ } ;
Original file line number Diff line number Diff line change
1
+ class Solution (object ):
2
+ def countPrimeSetBits (self , L , R ):
3
+ """
4
+ :type L: int
5
+ :type R: int
6
+ :rtype: int
7
+ """
8
+ count = 0
9
+ for i in range (L , R + 1 ):
10
+ one_count = 0
11
+ while i != 0 :
12
+ i &= i - 1
13
+ one_count += 1
14
+
15
+ # 1 is not a prime
16
+ if one_count == 1 :
17
+ continue
18
+
19
+ # judge is prime
20
+ x = one_count / 2
21
+ while x > 1 :
22
+ if one_count % x == 0 :
23
+ break
24
+ else :
25
+ x -= 1
26
+ else :
27
+ count += 1
28
+
29
+ return count
You can’t perform that action at this time.
0 commit comments