Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions SOLVED.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
| 744 | [Find Smallest Letter Greater Than Target](https://leetcode.com/problems/find-smallest-letter-greater-than-target/) | [Code](src/leetcode/problems/find-smallest-letter-greater-than-target.cpp) / [Test](test/leetcode/problems/find-smallest-letter-greater-than-target.cpp) |
| 756 | [Pyramid Transition Matrix](https://leetcode.com/problems/pyramid-transition-matrix/) | [Code](src/leetcode/problems/pyramid-transition-matrix.cpp) / [Test](test/leetcode/problems/pyramid-transition-matrix.cpp) |
| 761 | [Special Binary String](https://leetcode.com/problems/special-binary-string/) | [Code](src/leetcode/problems/special-binary-string.cpp) / [Test](test/leetcode/problems/special-binary-string.cpp) |
| 762 | [Prime Number of Set Bits in Binary Representation](https://leetcode.com/problems/prime-number-of-set-bits-in-binary-representation/) | [Code](src/leetcode/problems/prime-number-of-set-bits-in-binary-representation.cpp) / [Test](test/leetcode/problems/prime-number-of-set-bits-in-binary-representation.cpp) |
| 799 | [Champagne Tower](https://leetcode.com/problems/champagne-tower/) | [Code](src/leetcode/problems/champagne-tower.cpp) / [Test](test/leetcode/problems/champagne-tower.cpp) |
| 816 | [Ambiguous Coordinates](https://leetcode.com/problems/ambiguous-coordinates/) | [Code](src/leetcode/problems/ambiguous-coordinates.cpp) / [Test](test/leetcode/problems/ambiguous-coordinates.cpp) |
| 829 | [Consecutive Numbers Sum](https://leetcode.com/problems/consecutive-numbers-sum/) | [Code](src/leetcode/problems/consecutive-numbers-sum.cpp) / [Test](test/leetcode/problems/consecutive-numbers-sum.cpp) |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

#include "leetcode/core.h"

namespace leetcode {
namespace problem_762 {

using Func = std::function<int(int, int)>;

class PrimeNumberOfSetBitsInBinaryRepresentationSolution : public SolutionBase<Func> {
public:
//! 762. Prime Number of Set Bits in Binary Representation
//! https://leetcode.com/problems/prime-number-of-set-bits-in-binary-representation/
int countPrimeSetBits(int left, int right);

PrimeNumberOfSetBitsInBinaryRepresentationSolution();
};

} // namespace problem_762
} // namespace leetcode
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@

#include "leetcode/problems/prime-number-of-set-bits-in-binary-representation.h"

namespace leetcode {
namespace problem_762 {

// 判断是否为素数(只处理小数字,因为 set bits 最多约 20 个)
static bool isPrime(int n) {
if (n < 2) return false;
if (n == 2) return true;
if (n % 2 == 0) return false;
for (int i = 3; i * i <= n; i += 2) {
if (n % i == 0) return false;
}
return true;
}

// 遍历每个数字,统计二进制中素数个 1 的数字个数
// 时间复杂度: O((right - left + 1) * log(right)),空间复杂度: O(1)
static int solution1(int left, int right) {
int count = 0;
for (int num = left; num <= right; ++num) {
int setBits = __builtin_popcount(num);
if (isPrime(setBits)) {
++count;
}
}
return count;
}

// 使用位运算优化版本
// 时间复杂度: O((right - left + 1) * log(right)),空间复杂度: O(1)
static int solution2(int left, int right) {
int count = 0;
for (int num = left; num <= right; ++num) {
int n = num;
int setBits = 0;
while (n) {
setBits += (n & 1);
n >>= 1;
}
if (isPrime(setBits)) {
++count;
}
}
return count;
}

// 使用打表优化素数判断(set bits 最多约 20 个)
// 时间复杂度: O(right - left + 1),空间复杂度: O(1)
static int solution3(int left, int right) {
// 1-20 中的素数集合:2, 3, 5, 7, 11, 13, 17, 19
// 使用位掩码表示:第 i 位为 1 表示 i 是素数
const int primeMask = (1 << 2) | (1 << 3) | (1 << 5) | (1 << 7) |
(1 << 11) | (1 << 13) | (1 << 17) | (1 << 19);
int count = 0;
for (int num = left; num <= right; ++num) {
int setBits = __builtin_popcount(num);
if (primeMask & (1 << setBits)) {
++count;
}
}
return count;
}

PrimeNumberOfSetBitsInBinaryRepresentationSolution::PrimeNumberOfSetBitsInBinaryRepresentationSolution() {
setMetaInfo({.id = 762,
.title = "Prime Number of Set Bits in Binary Representation",
.url = "https://leetcode.com/problems/prime-number-of-set-bits-in-binary-representation/"});
registerStrategy("Builtin Popcount", solution1);
registerStrategy("Bit Manipulation", solution2);
registerStrategy("Prime Mask", solution3);
}

int PrimeNumberOfSetBitsInBinaryRepresentationSolution::countPrimeSetBits(int left, int right) {
return getSolution()(left, right);
}

} // namespace problem_762
} // namespace leetcode
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@

#include "leetcode/problems/prime-number-of-set-bits-in-binary-representation.h"

#include "gtest/gtest.h"

namespace leetcode {
namespace problem_762 {

class PrimeNumberOfSetBitsInBinaryRepresentationTest : public ::testing::TestWithParam<std::string> {
protected:
void SetUp() override { solution.setStrategy(GetParam()); }

PrimeNumberOfSetBitsInBinaryRepresentationSolution solution;
};

TEST_P(PrimeNumberOfSetBitsInBinaryRepresentationTest, Example1) {
int left = 6;
int right = 10;
int expected = 4;
EXPECT_EQ(expected, solution.countPrimeSetBits(left, right));
}

TEST_P(PrimeNumberOfSetBitsInBinaryRepresentationTest, Example2) {
int left = 10;
int right = 15;
int expected = 5;
EXPECT_EQ(expected, solution.countPrimeSetBits(left, right));
}

TEST_P(PrimeNumberOfSetBitsInBinaryRepresentationTest, SingleNumber) {
int left = 7;
int right = 7;
// 7 -> 111, 3 set bits, 3 is prime
int expected = 1;
EXPECT_EQ(expected, solution.countPrimeSetBits(left, right));
}

TEST_P(PrimeNumberOfSetBitsInBinaryRepresentationTest, RangeWithNoPrimeSetBits) {
int left = 1;
int right = 1;
// 1 -> 1, 1 set bit, 1 is not prime
int expected = 0;
EXPECT_EQ(expected, solution.countPrimeSetBits(left, right));
}

TEST_P(PrimeNumberOfSetBitsInBinaryRepresentationTest, PowerOfTwo) {
int left = 1;
int right = 8;
// 1 -> 1 (not prime)
// 2 -> 10 (1, not prime)
// 3 -> 11 (2, prime)
// 4 -> 100 (1, not prime)
// 5 -> 101 (2, prime)
// 6 -> 110 (2, prime)
// 7 -> 111 (3, prime)
// 8 -> 1000 (1, not prime)
int expected = 4;
EXPECT_EQ(expected, solution.countPrimeSetBits(left, right));
}

INSTANTIATE_TEST_SUITE_P(
LeetCode, PrimeNumberOfSetBitsInBinaryRepresentationTest,
::testing::ValuesIn(PrimeNumberOfSetBitsInBinaryRepresentationSolution().getStrategyNames()));

} // namespace problem_762
} // namespace leetcode