62. Unique Paths -Medium
来源:互联网 发布:.net软件开发工程师 编辑:程序博客网 时间:2024/05/22 15:02
Question
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?
Note: m and n will be at most 100.
一个机器人位于m x n的表格的左上角,它只会向右走或向下走。这个机器人想要走到表格的右下角,有几种方法。(m,n最多100)
Example
None
Solution
动态规划解。定义dp[i][j]:从左上角到grid[i][j]的方法数。递推式:dp[i][j] = dp[i - 1][j] + dp[i][j - 1]。即把来源的两个方向的方法数相加即可
class Solution(object): def uniquePaths(self, m, n): """ :type m: int :type n: int :rtype: int """ if m == 0 or n == 0: return 0 dp = [[0 for _ in range(n)] for _ in range(m)] for index_m in range(m): for index_n in range(n): # 第一行和第一列均只有一个方向,所以方法数都为1 if index_m == 0 or index_n == 0: dp[index_m][index_n] = 1 # 其余表格都有两个方向,相加即可 else: dp[index_m][index_n] = dp[index_m - 1][index_n] + dp[index_m][index_n - 1] return dp[-1][-1]
0 0
- 62. Unique Paths -Medium
- 62. Unique Paths Medium
- (Leetcode)62.Unique Paths(medium)
- LeetCode 62. Unique Paths (Medium)
- Leetcode 62. Unique Paths (Medium) (cpp)
- Leetcode 62. Unique Paths (Medium) (java)
- [Medium]Unique Paths II
- [Medium]Unique Paths
- [Leetcode 62, Medium] Unique Paths
- 【LeetCode】(63)Unique Paths (Medium)
- Medium 62题 Unique Paths
- 63. Unique Paths II -Medium
- 63. Unique Paths II Medium
- 63. Unique Paths II Medium
- 62. Unique Paths 类别:动态规划 难度:medium
- 动态规划——62. Unique Paths[medium]
- 第七周 leetcode 62. Unique Paths(Medium)
- Leetcode解题笔记 62. Unique Paths [Medium] 动态规划
- 编译新版本spice-gtk
- 226. Invert Binary Tree | 翻转二叉树
- OpenCV on Mac OSX: A step-by-step guide
- dlnd-your-first-neural-network中反向传播的笔记
- oracle简单命令
- 62. Unique Paths -Medium
- c#虚拟路径
- 5.CSS图像绘制之:条纹背景
- 栈与队列(六)之总结篇
- linux下修改oracle密码报错修改密码问题解决
- python基础-------类
- 阶段复习——第一个年终总结
- UVA 1149 Bin Packing 装箱(简单贪心)
- CXF框架的使用,利用cxf开发webservice(六)