Skip to content
Open
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 @@ -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) |
Expand Down
18 changes: 18 additions & 0 deletions include/leetcode/problems/sort-integers-by-the-number-of-1-bits.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "leetcode/core.h"

namespace leetcode {
namespace problem_1356 {

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

class SortIntegersByTheNumberOf1BitsSolution : public SolutionBase<Func> {
public:
//! 1356. Sort Integers by The Number of 1 Bits
//! https://leetcode.com/problems/sort-integers-by-the-number-of-1-bits/
vector<int> sortByBits(vector<int>& arr);

SortIntegersByTheNumberOf1BitsSolution();
};

} // namespace problem_1356
} // namespace leetcode
44 changes: 44 additions & 0 deletions src/leetcode/problems/sort-integers-by-the-number-of-1-bits.cpp
Original file line number Diff line number Diff line change
@@ -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<int> solution1(vector<int>& arr) {
vector<int> 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<int> SortIntegersByTheNumberOf1BitsSolution::sortByBits(vector<int>& arr) {
return getSolution()(arr);
}

} // namespace problem_1356
} // namespace leetcode
34 changes: 34 additions & 0 deletions test/leetcode/problems/sort-integers-by-the-number-of-1-bits.cpp
Original file line number Diff line number Diff line change
@@ -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<std::string> {
protected:
void SetUp() override { solution.setStrategy(GetParam()); }

SortIntegersByTheNumberOf1BitsSolution solution;
};

TEST_P(SortIntegersByTheNumberOf1BitsTest, Example1) {
vector<int> arr = {0, 1, 2, 3, 4, 5, 6, 7, 8};
vector<int> expected = {0, 1, 2, 4, 8, 3, 5, 6, 7};
vector<int> result = solution.sortByBits(arr);
EXPECT_EQ(expected, result);
}

TEST_P(SortIntegersByTheNumberOf1BitsTest, Example2) {
vector<int> arr = {1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1};
vector<int> expected = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024};
vector<int> result = solution.sortByBits(arr);
EXPECT_EQ(expected, result);
}

INSTANTIATE_TEST_SUITE_P(
LeetCode, SortIntegersByTheNumberOf1BitsTest,
::testing::ValuesIn(SortIntegersByTheNumberOf1BitsSolution().getStrategyNames()));

} // namespace problem_1356
} // namespace leetcode