Skip to content

Commit 330a2c2

Browse files
author
kwu130
committed
feat(src): add 64-minPathSum, 66-plusOne, 67-addBinary
1 parent 1dea885 commit 330a2c2

3 files changed

Lines changed: 131 additions & 0 deletions

File tree

‎src/64-minPathSum.cpp‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//
2+
// Created by 1412 on 2025/11/8.
3+
//
4+
5+
#include <bits/stdc++.h>
6+
7+
using namespace std;
8+
9+
class Solution {
10+
public:
11+
int minPathSum(vector<vector<int>>& grid) {
12+
const int m = grid.size();
13+
const int n = grid[0].size();
14+
vector<vector<int>> dp(m, vector<int>(n, 0));
15+
dp[0][0] = grid[0][0];
16+
for (int i = 1; i < m; ++i) {
17+
dp[i][0] = dp[i - 1][0] + grid[i][0];
18+
}
19+
for (int j = 1; j < n; ++j) {
20+
dp[0][j] = dp[0][j - 1] + grid[0][j];
21+
}
22+
for (int i = 1; i < m; ++i) {
23+
for (int j = 1; j < n; ++j) {
24+
dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j];
25+
}
26+
}
27+
return dp[m - 1][n - 1];
28+
}
29+
};
30+
31+
void test() {
32+
Solution s;
33+
vector<vector<int>> grid = {
34+
{1, 3, 1},
35+
{1, 5, 1},
36+
{4, 2, 1}
37+
};
38+
assert(s.minPathSum(grid) == 7);
39+
40+
grid = {
41+
{1, 2, 3},
42+
{4, 5, 6}
43+
};
44+
assert(s.minPathSum(grid) == 12);
45+
}
46+
47+
int main() {
48+
test();
49+
return 0;
50+
}

‎src/66-plusOne.cpp‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//
2+
// Created by 1412 on 2025/11/8.
3+
//
4+
5+
#include <bits/stdc++.h>
6+
7+
using namespace std;
8+
9+
class Solution {
10+
public:
11+
vector<int> plusOne(vector<int>& digits) {
12+
for (int i = digits.size() - 1; i >= 0; --i) {
13+
if (digits[i] < 9) {
14+
digits[i]++;
15+
return digits;
16+
}
17+
digits[i] = 0;
18+
}
19+
digits.insert(digits.begin(), 1);
20+
return digits;
21+
}
22+
};
23+
24+
void test() {
25+
Solution s;
26+
vector<int> digits = {1, 2, 3};
27+
vector<int> expected = {1, 2, 4};
28+
vector<int> res = s.plusOne(digits);
29+
assert(res == expected);
30+
31+
digits = {4, 3, 2, 1};
32+
expected = {4, 3, 2, 2};
33+
res = s.plusOne(digits);
34+
assert(res == expected);
35+
36+
digits = {9};
37+
expected = {1, 0};
38+
res = s.plusOne(digits);
39+
assert(res == expected);
40+
}
41+
42+
int main() {
43+
test();
44+
return 0;
45+
}

‎src/67-addBinary.cpp‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// Created by 1412 on 2025/11/8.
3+
//
4+
5+
#include <bits/stdc++.h>
6+
7+
using namespace std;
8+
9+
class Solution {
10+
public:
11+
string addBinary(string a, string b) {
12+
string res;
13+
int i = a.size() - 1, j = b.size() - 1, carry = 0;
14+
while (i >= 0 || j >= 0 || carry) {
15+
const int x = i >= 0 ? a[i--] - '0' : 0;
16+
const int y = j >= 0 ? b[j--] - '0' : 0;
17+
const int sum = x + y + carry;
18+
res.push_back(sum % 2 + '0');
19+
carry = sum / 2;
20+
}
21+
ranges::reverse(res);
22+
return res;
23+
}
24+
};
25+
26+
void test() {
27+
Solution s;
28+
assert(s.addBinary("11", "1") == "100");
29+
assert(s.addBinary("1010", "1011") == "10101");
30+
assert(s.addBinary("0", "0") == "0");
31+
}
32+
33+
int main() {
34+
test();
35+
return 0;
36+
}

0 commit comments

Comments
 (0)