We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent fc5e5d1 commit cb162c8Copy full SHA for cb162c8
dy.unique-paths.py
@@ -0,0 +1,29 @@
1
+class Solution:
2
+ """
3
+ 62. 不同路径
4
+ https://leetcode-cn.com/problems/unique-paths/
5
+ 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” )。
6
+ 机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Finish”)。
7
+ 问总共有多少条不同的路径?
8
9
+ def uniquePaths(self, m: int, n: int) -> int:
10
+ # 生成缓存数组
11
+ dp = [[1] * n] + [[1] + [0] * (n - 1) for _ in range(m - 1)]
12
+ print(dp)
13
+ for i in range(1, m):
14
+ for j in range(1, n):
15
+ dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
16
+ return dp[-1][-1]
17
+
18
+ def uniquePathsByDy(self, m: int, n: int) -> int:
19
+ if m == 1:
20
+ return 1
21
22
+ if n == 1:
23
24
25
+ return self.uniquePaths(m - 1, n) + self.uniquePaths(m, n - 1)
26
27
28
+so = Solution()
29
+print(so.uniquePaths(7, 3))
0 commit comments