From f6dbb7425e811611fe81421ac5c22b2fa14327f7 Mon Sep 17 00:00:00 2001 From: 0xMashiro <0xMashiro@users.noreply.github.com> Date: Sat, 21 Feb 2026 03:10:35 +0000 Subject: [PATCH] feat: Add solution for LeetCode #762 --- SOLVED.md | 1 + ...ber-of-set-bits-in-binary-representation.h | 19 +++++ ...r-of-set-bits-in-binary-representation.cpp | 80 +++++++++++++++++++ ...r-of-set-bits-in-binary-representation.cpp | 66 +++++++++++++++ 4 files changed, 166 insertions(+) create mode 100644 include/leetcode/problems/prime-number-of-set-bits-in-binary-representation.h create mode 100644 src/leetcode/problems/prime-number-of-set-bits-in-binary-representation.cpp create mode 100644 test/leetcode/problems/prime-number-of-set-bits-in-binary-representation.cpp diff --git a/SOLVED.md b/SOLVED.md index 8a07c86..ce0c746 100644 --- a/SOLVED.md +++ b/SOLVED.md @@ -90,6 +90,7 @@ | 740 | [Delete and Earn](https://leetcode.com/problems/delete-and-earn/) | [Code](src/leetcode/problems/delete-and-earn.cpp) / [Test](test/leetcode/problems/delete-and-earn.cpp) | | 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) | +| 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) | diff --git a/include/leetcode/problems/prime-number-of-set-bits-in-binary-representation.h b/include/leetcode/problems/prime-number-of-set-bits-in-binary-representation.h new file mode 100644 index 0000000..86fecd1 --- /dev/null +++ b/include/leetcode/problems/prime-number-of-set-bits-in-binary-representation.h @@ -0,0 +1,19 @@ + +#include "leetcode/core.h" + +namespace leetcode { +namespace problem_762 { + +using Func = std::function; + +class PrimeNumberOfSetBitsInBinaryRepresentationSolution : public SolutionBase { + 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 diff --git a/src/leetcode/problems/prime-number-of-set-bits-in-binary-representation.cpp b/src/leetcode/problems/prime-number-of-set-bits-in-binary-representation.cpp new file mode 100644 index 0000000..d7aab7a --- /dev/null +++ b/src/leetcode/problems/prime-number-of-set-bits-in-binary-representation.cpp @@ -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 diff --git a/test/leetcode/problems/prime-number-of-set-bits-in-binary-representation.cpp b/test/leetcode/problems/prime-number-of-set-bits-in-binary-representation.cpp new file mode 100644 index 0000000..9f3745e --- /dev/null +++ b/test/leetcode/problems/prime-number-of-set-bits-in-binary-representation.cpp @@ -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 { + 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