From ee9ef94bc228f0c954c84ba9cc3faec1532648c8 Mon Sep 17 00:00:00 2001 From: 0xMashiro <0xMashiro@users.noreply.github.com> Date: Wed, 25 Feb 2026 03:24:19 +0000 Subject: [PATCH] feat: Add solution for LeetCode #1356 --- SOLVED.md | 1 + .../sort-integers-by-the-number-of-1-bits.h | 18 ++++++++ .../sort-integers-by-the-number-of-1-bits.cpp | 44 +++++++++++++++++++ .../sort-integers-by-the-number-of-1-bits.cpp | 34 ++++++++++++++ 4 files changed, 97 insertions(+) create mode 100644 include/leetcode/problems/sort-integers-by-the-number-of-1-bits.h create mode 100644 src/leetcode/problems/sort-integers-by-the-number-of-1-bits.cpp create mode 100644 test/leetcode/problems/sort-integers-by-the-number-of-1-bits.cpp diff --git a/SOLVED.md b/SOLVED.md index d56b59e..8c31f2f 100644 --- a/SOLVED.md +++ b/SOLVED.md @@ -146,6 +146,7 @@ | 1325 | [Delete Leaves With a Given Value](https://leetcode.com/problems/delete-leaves-with-a-given-value/) | [Code](src/leetcode/problems/delete-leaves-with-a-given-value.cpp) / [Test](test/leetcode/problems/delete-leaves-with-a-given-value.cpp) | | 1331 | [Rank Transform of an Array](https://leetcode.com/problems/rank-transform-of-an-array/) | [Code](src/leetcode/problems/rank-transform-of-an-array.cpp) / [Test](test/leetcode/problems/rank-transform-of-an-array.cpp) | | 1351 | [Count Negative Numbers in a Sorted Matrix](https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/) | [Code](src/leetcode/problems/count-negative-numbers-in-a-sorted-matrix.cpp) / [Test](test/leetcode/problems/count-negative-numbers-in-a-sorted-matrix.cpp) | +| 1356 | [Sort Integers by The Number of 1 Bits](https://leetcode.com/problems/sort-integers-by-the-number-of-1-bits/) | [Code](src/leetcode/problems/sort-integers-by-the-number-of-1-bits.cpp) / [Test](test/leetcode/problems/sort-integers-by-the-number-of-1-bits.cpp) | | 1357 | [Apply Discount Every n Orders](https://leetcode.com/problems/apply-discount-every-n-orders/) | [Code](src/leetcode/problems/apply-discount-every-n-orders.cpp) / [Test](test/leetcode/problems/apply-discount-every-n-orders.cpp) | | 1358 | [Number of Substrings Containing All Three Characters](https://leetcode.com/problems/number-of-substrings-containing-all-three-characters/) | [Code](src/leetcode/problems/number-of-substrings-containing-all-three-characters.cpp) / [Test](test/leetcode/problems/number-of-substrings-containing-all-three-characters.cpp) | | 1365 | [How Many Numbers Are Smaller Than the Current Number](https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/) | [Code](src/leetcode/problems/how-many-numbers-are-smaller-than-the-current-number.cpp) / [Test](test/leetcode/problems/how-many-numbers-are-smaller-than-the-current-number.cpp) | diff --git a/include/leetcode/problems/sort-integers-by-the-number-of-1-bits.h b/include/leetcode/problems/sort-integers-by-the-number-of-1-bits.h new file mode 100644 index 0000000..8315d76 --- /dev/null +++ b/include/leetcode/problems/sort-integers-by-the-number-of-1-bits.h @@ -0,0 +1,18 @@ +#include "leetcode/core.h" + +namespace leetcode { +namespace problem_1356 { + +using Func = std::function(vector&)>; + +class SortIntegersByTheNumberOf1BitsSolution : public SolutionBase { + public: + //! 1356. Sort Integers by The Number of 1 Bits + //! https://leetcode.com/problems/sort-integers-by-the-number-of-1-bits/ + vector sortByBits(vector& arr); + + SortIntegersByTheNumberOf1BitsSolution(); +}; + +} // namespace problem_1356 +} // namespace leetcode diff --git a/src/leetcode/problems/sort-integers-by-the-number-of-1-bits.cpp b/src/leetcode/problems/sort-integers-by-the-number-of-1-bits.cpp new file mode 100644 index 0000000..b92b221 --- /dev/null +++ b/src/leetcode/problems/sort-integers-by-the-number-of-1-bits.cpp @@ -0,0 +1,44 @@ +#include "leetcode/problems/sort-integers-by-the-number-of-1-bits.h" + +namespace leetcode { +namespace problem_1356 { + +// 计算整数二进制表示中 1 的个数 +static int countBits(int n) { + int count = 0; + while (n) { + count += n & 1; + n >>= 1; + } + return count; +} + +// 使用自定义比较器排序 +// 先按二进制 1 的个数排序,个数相同则按数值排序 +// 时间复杂度: O(n log n), 空间复杂度: O(log n) 或 O(n) +static vector solution1(vector& arr) { + vector result = arr; + sort(result.begin(), result.end(), [](int a, int b) { + int countA = countBits(a); + int countB = countBits(b); + if (countA != countB) { + return countA < countB; + } + return a < b; + }); + return result; +} + +SortIntegersByTheNumberOf1BitsSolution::SortIntegersByTheNumberOf1BitsSolution() { + setMetaInfo({.id = 1356, + .title = "Sort Integers by The Number of 1 Bits", + .url = "https://leetcode.com/problems/sort-integers-by-the-number-of-1-bits/"}); + registerStrategy("Custom Comparator", solution1); +} + +vector SortIntegersByTheNumberOf1BitsSolution::sortByBits(vector& arr) { + return getSolution()(arr); +} + +} // namespace problem_1356 +} // namespace leetcode diff --git a/test/leetcode/problems/sort-integers-by-the-number-of-1-bits.cpp b/test/leetcode/problems/sort-integers-by-the-number-of-1-bits.cpp new file mode 100644 index 0000000..807268b --- /dev/null +++ b/test/leetcode/problems/sort-integers-by-the-number-of-1-bits.cpp @@ -0,0 +1,34 @@ +#include "leetcode/problems/sort-integers-by-the-number-of-1-bits.h" + +#include "gtest/gtest.h" + +namespace leetcode { +namespace problem_1356 { + +class SortIntegersByTheNumberOf1BitsTest : public ::testing::TestWithParam { + protected: + void SetUp() override { solution.setStrategy(GetParam()); } + + SortIntegersByTheNumberOf1BitsSolution solution; +}; + +TEST_P(SortIntegersByTheNumberOf1BitsTest, Example1) { + vector arr = {0, 1, 2, 3, 4, 5, 6, 7, 8}; + vector expected = {0, 1, 2, 4, 8, 3, 5, 6, 7}; + vector result = solution.sortByBits(arr); + EXPECT_EQ(expected, result); +} + +TEST_P(SortIntegersByTheNumberOf1BitsTest, Example2) { + vector arr = {1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1}; + vector expected = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024}; + vector result = solution.sortByBits(arr); + EXPECT_EQ(expected, result); +} + +INSTANTIATE_TEST_SUITE_P( + LeetCode, SortIntegersByTheNumberOf1BitsTest, + ::testing::ValuesIn(SortIntegersByTheNumberOf1BitsSolution().getStrategyNames())); + +} // namespace problem_1356 +} // namespace leetcode