Skip to content

Commit 2c1bafc

Browse files
committed
feat: add solutions to lcof2 problems: No.098,099
1 parent e7143c3 commit 2c1bafc

File tree

19 files changed

+446
-23
lines changed

19 files changed

+446
-23
lines changed

lcof2/剑指 Offer II 098. 路径的数目/README.md

+78-1
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,99 @@
6363

6464
<!-- 这里可写通用的实现逻辑 -->
6565

66+
动态规划。
67+
68+
假设 `dp[i][j]` 表示到达网格 `(i,j)` 的路径数,则 `dp[i][j] = dp[i - 1][j] + dp[i][j - 1]`
69+
6670
<!-- tabs:start -->
6771

6872
### **Python3**
6973

7074
<!-- 这里可写当前语言的特殊实现逻辑 -->
7175

7276
```python
73-
77+
class Solution:
78+
def uniquePaths(self, m: int, n: int) -> int:
79+
dp = [[1] * n for _ in range(m)]
80+
for i in range(1, m):
81+
for j in range(1, n):
82+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
83+
return dp[-1][-1]
7484
```
7585

7686
### **Java**
7787

7888
<!-- 这里可写当前语言的特殊实现逻辑 -->
7989

8090
```java
91+
class Solution {
92+
public int uniquePaths(int m, int n) {
93+
int[][] dp = new int[m][n];
94+
for (int i = 0; i < m; ++i) {
95+
Arrays.fill(dp[i], 1);
96+
}
97+
for (int i = 1; i < m; ++i) {
98+
for (int j = 1; j < n; ++j) {
99+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
100+
}
101+
}
102+
return dp[m - 1][n - 1];
103+
}
104+
}
105+
```
106+
107+
### **TypeScript**
108+
109+
```ts
110+
function uniquePaths(m: number, n: number): number {
111+
let dp = Array.from({length: m}, v => new Array(n).fill(1));
112+
for (let i = 1; i < m; ++i) {
113+
for (let j = 1; j < n; ++j) {
114+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
115+
}
116+
}
117+
return dp[m-1][n-1];
118+
};
119+
```
120+
121+
### **C++**
122+
123+
```cpp
124+
class Solution {
125+
public:
126+
int uniquePaths(int m, int n) {
127+
vector<vector<int>> dp(m, vector<int>(n, 1));
128+
for (int i = 1; i < m; ++i)
129+
{
130+
for (int j = 1; j < n; ++j)
131+
{
132+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
133+
}
134+
}
135+
return dp[m - 1][n - 1];
136+
}
137+
};
138+
```
81139
140+
### **Go**
141+
142+
```go
143+
func uniquePaths(m int, n int) int {
144+
dp := make([][]int, m)
145+
for i := 0; i < m; i++ {
146+
dp[i] = make([]int, n)
147+
}
148+
for i := 0; i < m; i++ {
149+
for j := 0; j < n; j++ {
150+
if i == 0 || j == 0 {
151+
dp[i][j] = 1
152+
} else {
153+
dp[i][j] = dp[i-1][j] + dp[i][j-1]
154+
}
155+
}
156+
}
157+
return dp[m-1][n-1]
158+
}
82159
```
83160

84161
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
int uniquePaths(int m, int n) {
4+
vector<vector<int>> dp(m, vector<int>(n, 1));
5+
for (int i = 1; i < m; ++i)
6+
{
7+
for (int j = 1; j < n; ++j)
8+
{
9+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
10+
}
11+
}
12+
return dp[m - 1][n - 1];
13+
}
14+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
func uniquePaths(m int, n int) int {
2+
dp := make([][]int, m)
3+
for i := 0; i < m; i++ {
4+
dp[i] = make([]int, n)
5+
}
6+
for i := 0; i < m; i++ {
7+
for j := 0; j < n; j++ {
8+
if i == 0 || j == 0 {
9+
dp[i][j] = 1
10+
} else {
11+
dp[i][j] = dp[i-1][j] + dp[i][j-1]
12+
}
13+
}
14+
}
15+
return dp[m-1][n-1]
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public int uniquePaths(int m, int n) {
3+
int[][] dp = new int[m][n];
4+
for (int i = 0; i < m; ++i) {
5+
Arrays.fill(dp[i], 1);
6+
}
7+
for (int i = 1; i < m; ++i) {
8+
for (int j = 1; j < n; ++j) {
9+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
10+
}
11+
}
12+
return dp[m - 1][n - 1];
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def uniquePaths(self, m: int, n: int) -> int:
3+
dp = [[1] * n for _ in range(m)]
4+
for i in range(1, m):
5+
for j in range(1, n):
6+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
7+
return dp[-1][-1]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function uniquePaths(m: number, n: number): number {
2+
let dp = Array.from({length: m}, v => new Array(n).fill(1));
3+
for (let i = 1; i < m; ++i) {
4+
for (let j = 1; j < n; ++j) {
5+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
6+
}
7+
}
8+
return dp[m-1][n-1];
9+
};

lcof2/剑指 Offer II 099. 最小路径之和/README.md

+146-1
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,167 @@
4747

4848
<!-- 这里可写通用的实现逻辑 -->
4949

50+
动态规划。假设 `dp[i][j]` 表示到达网格 `(i,j)` 的最小数字和,先初始化 dp 第一列和第一行的所有值,然后利用递推公式:`dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j]` 求得 dp。
51+
52+
最后返回 `dp[m - 1][n - 1]` 即可。
53+
5054
<!-- tabs:start -->
5155

5256
### **Python3**
5357

5458
<!-- 这里可写当前语言的特殊实现逻辑 -->
5559

5660
```python
57-
61+
class Solution:
62+
def minPathSum(self, grid: List[List[int]]) -> int:
63+
m, n = len(grid), len(grid[0])
64+
dp = [[grid[0][0]] * n for _ in range(m)]
65+
for i in range(1, m):
66+
dp[i][0] = dp[i - 1][0] + grid[i][0]
67+
for j in range(1, n):
68+
dp[0][j] = dp[0][j - 1] + grid[0][j]
69+
for i in range(1, m):
70+
for j in range(1, n):
71+
dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j]
72+
return dp[-1][-1]
5873
```
5974

6075
### **Java**
6176

6277
<!-- 这里可写当前语言的特殊实现逻辑 -->
6378

6479
```java
80+
class Solution {
81+
public int minPathSum(int[][] grid) {
82+
int m = grid.length, n = grid[0].length;
83+
int[][] dp = new int[m][n];
84+
dp[0][0] = grid[0][0];
85+
for (int i = 1; i < m; ++i) {
86+
dp[i][0] = dp[i - 1][0] + grid[i][0];
87+
}
88+
for (int j = 1; j < n; ++j) {
89+
dp[0][j] = dp[0][j - 1] + grid[0][j];
90+
}
91+
for (int i = 1; i < m; ++i) {
92+
for (int j = 1; j < n; ++j) {
93+
dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j];
94+
}
95+
}
96+
return dp[m - 1][n - 1];
97+
}
98+
}
99+
```
100+
101+
### **TypeScript**
102+
103+
```ts
104+
function minPathSum(grid: number[][]): number {
105+
let m = grid.length, n = grid[0].length;
106+
let dp = Array.from({ length: m}, v => new Array(n).fill(0));
107+
dp[0][0] = grid[0][0];
108+
for (let i = 1; i < m; ++i) {
109+
dp[i][0] = dp[i - 1][0] + grid[i][0];
110+
}
111+
for (let j = 1; j < n; ++j) {
112+
dp[0][j] = dp[0][j - 1] + grid[0][j];
113+
}
114+
// dp
115+
for (let i = 1; i < m; ++i) {
116+
for (let j = 1; j < n; ++j) {
117+
let cur = grid[i][j];
118+
dp[i][j] = cur + Math.min(dp[i - 1][j], dp[i][j - 1]);
119+
}
120+
}
121+
return dp[m - 1][n - 1];
122+
};
123+
```
124+
125+
### **C++**
126+
127+
```cpp
128+
class Solution {
129+
public:
130+
int minPathSum(vector<vector<int>> &grid) {
131+
int m = grid.size(), n = grid[0].size();
132+
vector<vector<int>> dp(m, vector<int>(n, grid[0][0]));
133+
for (int i = 1; i < m; ++i)
134+
{
135+
dp[i][0] = dp[i - 1][0] + grid[i][0];
136+
}
137+
for (int j = 1; j < n; ++j)
138+
{
139+
dp[0][j] = dp[0][j - 1] + grid[0][j];
140+
}
141+
for (int i = 1; i < m; ++i)
142+
{
143+
for (int j = 1; j < n; ++j)
144+
{
145+
dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j];
146+
}
147+
}
148+
return dp[m - 1][n - 1];
149+
}
150+
};
151+
```
152+
153+
### **Go**
154+
155+
```go
156+
func minPathSum(grid [][]int) int {
157+
m, n := len(grid), len(grid[0])
158+
dp := make([][]int, m)
159+
for i := 0; i < m; i++ {
160+
dp[i] = make([]int, n)
161+
}
162+
dp[0][0] = grid[0][0]
163+
for i := 1; i < m; i++ {
164+
dp[i][0] = dp[i-1][0] + grid[i][0]
165+
}
166+
for j := 1; j < n; j++ {
167+
dp[0][j] = dp[0][j-1] + grid[0][j]
168+
}
169+
for i := 1; i < m; i++ {
170+
for j := 1; j < n; j++ {
171+
dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + grid[i][j]
172+
}
173+
}
174+
return dp[m-1][n-1]
175+
}
176+
177+
func min(a, b int) int {
178+
if a < b {
179+
return a
180+
}
181+
return b
182+
}
183+
```
65184

185+
### **C#**
186+
187+
```cs
188+
public class Solution {
189+
public int MinPathSum(int[][] grid) {
190+
int m = grid.Length, n = grid[0].Length;
191+
int[,] dp = new int[m, n];
192+
dp[0, 0] = grid[0][0];
193+
for (int i = 1; i < m; ++i)
194+
{
195+
dp[i, 0] = dp[i - 1, 0] + grid[i][0];
196+
}
197+
for (int j = 1; j < n; ++j)
198+
{
199+
dp[0, j] = dp[0, j - 1] + grid[0][j];
200+
}
201+
for (int i = 1; i < m; ++i)
202+
{
203+
for (int j = 1; j < n; ++j)
204+
{
205+
dp[i, j] = Math.Min(dp[i - 1, j], dp[i, j - 1]) + grid[i][j];
206+
}
207+
}
208+
return dp[m- 1, n - 1];
209+
}
210+
}
66211
```
67212

68213
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
int minPathSum(vector<vector<int>> &grid) {
4+
int m = grid.size(), n = grid[0].size();
5+
vector<vector<int>> dp(m, vector<int>(n, grid[0][0]));
6+
for (int i = 1; i < m; ++i)
7+
{
8+
dp[i][0] = dp[i - 1][0] + grid[i][0];
9+
}
10+
for (int j = 1; j < n; ++j)
11+
{
12+
dp[0][j] = dp[0][j - 1] + grid[0][j];
13+
}
14+
for (int i = 1; i < m; ++i)
15+
{
16+
for (int j = 1; j < n; ++j)
17+
{
18+
dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j];
19+
}
20+
}
21+
return dp[m - 1][n - 1];
22+
}
23+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
public class Solution {
2+
public int MinPathSum(int[][] grid) {
3+
int m = grid.Length, n = grid[0].Length;
4+
int[,] dp = new int[m, n];
5+
dp[0, 0] = grid[0][0];
6+
for (int i = 1; i < m; ++i)
7+
{
8+
dp[i, 0] = dp[i - 1, 0] + grid[i][0];
9+
}
10+
for (int j = 1; j < n; ++j)
11+
{
12+
dp[0, j] = dp[0, j - 1] + grid[0][j];
13+
}
14+
for (int i = 1; i < m; ++i)
15+
{
16+
for (int j = 1; j < n; ++j)
17+
{
18+
dp[i, j] = Math.Min(dp[i - 1, j], dp[i, j - 1]) + grid[i][j];
19+
}
20+
}
21+
return dp[m- 1, n - 1];
22+
}
23+
}

0 commit comments

Comments
 (0)