Skip to content

Commit e8a6931

Browse files
committed
feat: Add solution for LeetCode #868
1 parent be58e07 commit e8a6931

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed

SOLVED.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
| 848 | [Shifting Letters](https://leetcode.com/problems/shifting-letters/) | [Code](src/leetcode/problems/shifting-letters.cpp) / [Test](test/leetcode/problems/shifting-letters.cpp) |
102102
| 865 | [Smallest Subtree with all the Deepest Nodes](https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/) | [Code](src/leetcode/problems/smallest-subtree-with-all-the-deepest-nodes.cpp) / [Test](test/leetcode/problems/smallest-subtree-with-all-the-deepest-nodes.cpp) |
103103
| 867 | [Transpose Matrix](https://leetcode.com/problems/transpose-matrix/) | [Code](src/leetcode/problems/transpose-matrix.cpp) / [Test](test/leetcode/problems/transpose-matrix.cpp) |
104+
| 868 | [Binary Gap](https://leetcode.com/problems/binary-gap/) | [Code](src/leetcode/problems/binary-gap.cpp) / [Test](test/leetcode/problems/binary-gap.cpp) |
104105
| 897 | [Increasing Order Search Tree](https://leetcode.com/problems/increasing-order-search-tree/) | [Code](src/leetcode/problems/increasing-order-search-tree.cpp) / [Test](test/leetcode/problems/increasing-order-search-tree.cpp) |
105106
| 898 | [Bitwise ORs of Subarrays](https://leetcode.com/problems/bitwise-ors-of-subarrays/) | [Code](src/leetcode/problems/bitwise-ors-of-subarrays.cpp) / [Test](test/leetcode/problems/bitwise-ors-of-subarrays.cpp) |
106107
| 915 | [Partition Array into Disjoint Intervals](https://leetcode.com/problems/partition-array-into-disjoint-intervals/) | [Code](src/leetcode/problems/partition-array-into-disjoint-intervals.cpp) / [Test](test/leetcode/problems/partition-array-into-disjoint-intervals.cpp) |
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include "leetcode/core.h"
2+
3+
namespace leetcode {
4+
namespace problem_868 {
5+
6+
using Func = std::function<int(int)>;
7+
8+
class BinaryGapSolution : public SolutionBase<Func> {
9+
public:
10+
//! 868. Binary Gap
11+
//! https://leetcode.com/problems/binary-gap/
12+
int binaryGap(int n);
13+
14+
BinaryGapSolution();
15+
};
16+
17+
} // namespace problem_868
18+
} // namespace leetcode
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include "leetcode/problems/binary-gap.h"
2+
3+
namespace leetcode {
4+
namespace problem_868 {
5+
6+
// 位运算扫描:从低位到高位遍历,记录上一个1的位置
7+
// 时间复杂度: O(log n), 空间复杂度: O(1)
8+
static int solution(int n) {
9+
int lastPos = -1; // 上一个1的位置
10+
int maxDist = 0; // 最大距离
11+
int pos = 0; // 当前位位置
12+
13+
while (n > 0) {
14+
// 检查当前最低位是否为1
15+
if (n & 1) {
16+
if (lastPos != -1) {
17+
// 计算与上一个1的距离
18+
maxDist = max(maxDist, pos - lastPos);
19+
}
20+
lastPos = pos; // 更新上一个1的位置
21+
}
22+
n >>= 1; // 右移一位
23+
++pos;
24+
}
25+
26+
return maxDist;
27+
}
28+
29+
BinaryGapSolution::BinaryGapSolution() {
30+
setMetaInfo({.id = 868,
31+
.title = "Binary Gap",
32+
.url = "https://leetcode.com/problems/binary-gap/"});
33+
registerStrategy("Bit Scan", solution);
34+
}
35+
36+
int BinaryGapSolution::binaryGap(int n) {
37+
return getSolution()(n);
38+
}
39+
40+
} // namespace problem_868
41+
} // namespace leetcode
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include "leetcode/problems/binary-gap.h"
2+
3+
#include "gtest/gtest.h"
4+
5+
namespace leetcode {
6+
namespace problem_868 {
7+
8+
class BinaryGapTest : public ::testing::TestWithParam<std::string> {
9+
protected:
10+
void SetUp() override { solution.setStrategy(GetParam()); }
11+
12+
BinaryGapSolution solution;
13+
};
14+
15+
TEST_P(BinaryGapTest, Example1) {
16+
// n = 22, binary = "10110"
17+
EXPECT_EQ(2, solution.binaryGap(22));
18+
}
19+
20+
TEST_P(BinaryGapTest, Example2) {
21+
// n = 8, binary = "1000"
22+
EXPECT_EQ(0, solution.binaryGap(8));
23+
}
24+
25+
TEST_P(BinaryGapTest, Example3) {
26+
// n = 5, binary = "101"
27+
EXPECT_EQ(2, solution.binaryGap(5));
28+
}
29+
30+
TEST_P(BinaryGapTest, SingleBit) {
31+
// n = 1, binary = "1"
32+
EXPECT_EQ(0, solution.binaryGap(1));
33+
}
34+
35+
TEST_P(BinaryGapTest, AllOnes) {
36+
// n = 15, binary = "1111", adjacent pairs: (0,1), (1,2), (2,3) all distance 1
37+
EXPECT_EQ(1, solution.binaryGap(15));
38+
}
39+
40+
TEST_P(BinaryGapTest, LargeGap) {
41+
// n = 33, binary = "100001", gap = 5
42+
EXPECT_EQ(5, solution.binaryGap(33));
43+
}
44+
45+
INSTANTIATE_TEST_SUITE_P(
46+
LeetCode, BinaryGapTest,
47+
::testing::ValuesIn(BinaryGapSolution().getStrategyNames()));
48+
49+
} // namespace problem_868
50+
} // namespace leetcode

0 commit comments

Comments
 (0)