Skip to content

Commit bdea365

Browse files
committed
feat: Add solution for LeetCode #67
1 parent 8fd4b77 commit bdea365

File tree

4 files changed

+121
-0
lines changed

4 files changed

+121
-0
lines changed

SOLVED.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
| 62 | [Unique Paths](https://leetcode.com/problems/unique-paths/) | [Code](src/leetcode/problems/unique-paths.cpp) / [Test](test/leetcode/problems/unique-paths.cpp) |
1313
| 63 | [Unique Paths II](https://leetcode.com/problems/unique-paths-ii/) | [Code](src/leetcode/problems/unique-paths-ii.cpp) / [Test](test/leetcode/problems/unique-paths-ii.cpp) |
1414
| 66 | [Plus One](https://leetcode.com/problems/plus-one/) | [Code](src/leetcode/problems/plus-one.cpp) / [Test](test/leetcode/problems/plus-one.cpp) |
15+
| 67 | [Add Binary](https://leetcode.com/problems/add-binary/) | [Code](src/leetcode/problems/add-binary.cpp) / [Test](test/leetcode/problems/add-binary.cpp) |
1516
| 68 | [Text Justification](https://leetcode.com/problems/text-justification/) | [Code](src/leetcode/problems/text-justification.cpp) / [Test](test/leetcode/problems/text-justification.cpp) |
1617
| 71 | [Simplify Path](https://leetcode.com/problems/simplify-path/) | [Code](src/leetcode/problems/simplify-path.cpp) / [Test](test/leetcode/problems/simplify-path.cpp) |
1718
| 85 | [Maximal Rectangle](https://leetcode.com/problems/maximal-rectangle/) | [Code](src/leetcode/problems/maximal-rectangle.cpp) / [Test](test/leetcode/problems/maximal-rectangle.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_67 {
5+
6+
using Func = std::function<string(string, string)>;
7+
8+
class AddBinarySolution : public SolutionBase<Func> {
9+
public:
10+
//! 67. Add Binary
11+
//! https://leetcode.com/problems/add-binary/
12+
string addBinary(string a, string b);
13+
14+
AddBinarySolution();
15+
};
16+
17+
} // namespace problem_67
18+
} // namespace leetcode
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include "leetcode/problems/add-binary.h"
2+
3+
namespace leetcode {
4+
namespace problem_67 {
5+
6+
// 模拟二进制加法
7+
// 从字符串末尾开始逐位相加,处理进位
8+
// 时间复杂度: O(max(n, m)), 空间复杂度: O(max(n, m))
9+
static string solution1(string a, string b) {
10+
string result;
11+
int i = a.size() - 1;
12+
int j = b.size() - 1;
13+
int carry = 0;
14+
15+
// 从低位到高位逐位相加
16+
while (i >= 0 || j >= 0 || carry > 0) {
17+
int sum = carry;
18+
if (i >= 0) {
19+
sum += a[i] - '0';
20+
i--;
21+
}
22+
if (j >= 0) {
23+
sum += b[j] - '0';
24+
j--;
25+
}
26+
result.push_back((sum % 2) + '0');
27+
carry = sum / 2;
28+
}
29+
30+
// 反转结果(因为我们是从低位到高位计算的)
31+
reverse(result.begin(), result.end());
32+
return result;
33+
}
34+
35+
AddBinarySolution::AddBinarySolution() {
36+
setMetaInfo({.id = 67,
37+
.title = "Add Binary",
38+
.url = "https://leetcode.com/problems/add-binary/"});
39+
registerStrategy("Simulation", solution1);
40+
}
41+
42+
string AddBinarySolution::addBinary(string a, string b) {
43+
return getSolution()(a, b);
44+
}
45+
46+
} // namespace problem_67
47+
} // namespace leetcode
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include "leetcode/problems/add-binary.h"
2+
3+
#include "gtest/gtest.h"
4+
5+
namespace leetcode {
6+
namespace problem_67 {
7+
8+
class AddBinaryTest : public ::testing::TestWithParam<string> {
9+
protected:
10+
void SetUp() override { solution.setStrategy(GetParam()); }
11+
12+
AddBinarySolution solution;
13+
};
14+
15+
TEST_P(AddBinaryTest, Example1) {
16+
string a = "11";
17+
string b = "1";
18+
string expected = "100";
19+
EXPECT_EQ(expected, solution.addBinary(a, b));
20+
}
21+
22+
TEST_P(AddBinaryTest, Example2) {
23+
string a = "1010";
24+
string b = "1011";
25+
string expected = "10101";
26+
EXPECT_EQ(expected, solution.addBinary(a, b));
27+
}
28+
29+
TEST_P(AddBinaryTest, SimpleCase) {
30+
string a = "0";
31+
string b = "0";
32+
string expected = "0";
33+
EXPECT_EQ(expected, solution.addBinary(a, b));
34+
}
35+
36+
TEST_P(AddBinaryTest, DifferentLength) {
37+
string a = "1";
38+
string b = "111";
39+
string expected = "1000";
40+
EXPECT_EQ(expected, solution.addBinary(a, b));
41+
}
42+
43+
TEST_P(AddBinaryTest, CarryPropagation) {
44+
string a = "111";
45+
string b = "111";
46+
string expected = "1110";
47+
EXPECT_EQ(expected, solution.addBinary(a, b));
48+
}
49+
50+
INSTANTIATE_TEST_SUITE_P(
51+
LeetCode, AddBinaryTest,
52+
::testing::ValuesIn(AddBinarySolution().getStrategyNames()));
53+
54+
} // namespace problem_67
55+
} // namespace leetcode

0 commit comments

Comments
 (0)