Skip to content

Commit 6904286

Browse files
committedJan 4, 2023
Add solution #762
1 parent ee9f19c commit 6904286

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@
160160
722|[Remove Comments](./0722-remove-comments.js)|Medium|
161161
733|[Flood Fill](./0733-flood-fill.js)|Easy|
162162
739|[Daily Temperatures](./0739-daily-temperatures.js)|Medium|
163+
762|[Prime Number of Set Bits in Binary Representation](./0762-prime-number-of-set-bits-in-binary-representation.js)|Easy|
163164
784|[Letter Case Permutation](./0784-letter-case-permutation.js)|Medium|
164165
791|[Custom Sort String](./0791-custom-sort-string.js)|Medium|
165166
796|[Rotate String](./0796-rotate-string.js)|Easy|
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* 762. Prime Number of Set Bits in Binary Representation
3+
* https://leetcode.com/problems/prime-number-of-set-bits-in-binary-representation/
4+
* Difficulty: Easy
5+
*
6+
* Given two integers left and right, return the count of numbers in the inclusive range [left, right] having
7+
* a prime number of set bits in their binary representation.
8+
*
9+
* Recall that the number of set bits an integer has is the number of 1's present when written in binary.
10+
*
11+
* For example, 21 written in binary is 10101, which has 3 set bits.
12+
*/
13+
14+
/**
15+
* @param {number} left
16+
* @param {number} right
17+
* @return {number}
18+
*/
19+
var countPrimeSetBits = function(left, right) {
20+
const primes = new Set([2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]);
21+
let result = 0;
22+
23+
for (let i = left; i <= right; i++) {
24+
if (primes.has(i.toString(2).replace(/0+/g, '').length)) {
25+
result++;
26+
}
27+
}
28+
29+
return result;
30+
};

0 commit comments

Comments
 (0)
Please sign in to comment.