mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 00:25:22 +08:00
规范格式
This commit is contained in:
20
leetcode/0062.Unique-Paths/62. Unique Paths.go
Normal file
20
leetcode/0062.Unique-Paths/62. Unique Paths.go
Normal file
@ -0,0 +1,20 @@
|
||||
package leetcode
|
||||
|
||||
func uniquePaths(m int, n int) int {
|
||||
dp := make([][]int, n)
|
||||
for i := 0; i < n; i++ {
|
||||
dp[i] = make([]int, m)
|
||||
}
|
||||
for i := 0; i < m; i++ {
|
||||
dp[0][i] = 1
|
||||
}
|
||||
for i := 0; i < n; i++ {
|
||||
dp[i][0] = 1
|
||||
}
|
||||
for i := 1; i < n; i++ {
|
||||
for j := 1; j < m; j++ {
|
||||
dp[i][j] = dp[i-1][j] + dp[i][j-1]
|
||||
}
|
||||
}
|
||||
return dp[n-1][m-1]
|
||||
}
|
53
leetcode/0062.Unique-Paths/62. Unique Paths_test.go
Normal file
53
leetcode/0062.Unique-Paths/62. Unique Paths_test.go
Normal file
@ -0,0 +1,53 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question62 struct {
|
||||
para62
|
||||
ans62
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para62 struct {
|
||||
m int
|
||||
n int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans62 struct {
|
||||
one int
|
||||
}
|
||||
|
||||
func Test_Problem62(t *testing.T) {
|
||||
|
||||
qs := []question62{
|
||||
|
||||
question62{
|
||||
para62{3, 2},
|
||||
ans62{3},
|
||||
},
|
||||
|
||||
question62{
|
||||
para62{7, 3},
|
||||
ans62{28},
|
||||
},
|
||||
|
||||
question62{
|
||||
para62{1, 2},
|
||||
ans62{1},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 62------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans62, q.para62
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, uniquePaths(p.m, p.n))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
43
leetcode/0062.Unique-Paths/README.md
Executable file
43
leetcode/0062.Unique-Paths/README.md
Executable file
@ -0,0 +1,43 @@
|
||||
# [62. Unique Paths](https://leetcode.com/problems/unique-paths/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
A robot is located at the top-left corner of a *m* x *n* grid (marked 'Start' in the diagram below).
|
||||
|
||||
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
|
||||
|
||||
How many possible unique paths are there?
|
||||
|
||||

|
||||
|
||||
Above is a 7 x 3 grid. How many possible unique paths are there?
|
||||
|
||||
**Note:** *m* and *n* will be at most 100.
|
||||
|
||||
**Example 1:**
|
||||
|
||||
Input: m = 3, n = 2
|
||||
Output: 3
|
||||
Explanation:
|
||||
From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
|
||||
1. Right -> Right -> Down
|
||||
2. Right -> Down -> Right
|
||||
3. Down -> Right -> Right
|
||||
|
||||
**Example 2:**
|
||||
|
||||
Input: m = 7, n = 3
|
||||
Output: 28
|
||||
|
||||
|
||||
## 题目大意
|
||||
|
||||
一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” )。机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Finish”)。问总共有多少条不同的路径?
|
||||
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 这是一道简单的 DP 题。输出地图上从左上角走到右下角的走法数。
|
||||
- 由于机器人只能向右走和向下走,所以地图的第一行和第一列的走法数都是 1,地图中任意一点的走法数是 `dp[i][j] = dp[i-1][j] + dp[i][j-1]`
|
||||
|
Reference in New Issue
Block a user