leetcode 【 Unique Paths 】python 实现
题目:
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 3 x 7 grid. How many possible unique paths are there?
Note: m and n will be at most 100.
代码:oj测试通过 Runtime: 44 ms
class Solution:
# @return an integer
def uniquePaths(self, m, n):
# none case
if m < 1 or n < 1:
return 0
# special case
if m==1 or n==1 :
return 1 # dp
dp = [[0 for col in range(n)] for row in range(m)]
# the elements in frist row have only one avaialbe pre-node
for i in range(n):
dp[0][i]=1
# the elements in first column have only one avaialble pre-node
for i in range(m):
dp[i][0]=1
# iterator other elements in the 2D-matrix
for row in range(1,m):
for col in range(1,n):
dp[row][col]=dp[row-1][col]+dp[row][col-1] return dp[m-1][n-1]
思路:
动态规划经典题目,用迭代的方法解决。
1. 先处理none case和special case
2. 2D-matrix的第一行和第一列上的元素 只能从上面的元素或左边的元素达到,因此可以直接获得其值
3. 遍历其余的位置:每一个position只能由其左边或者上边的元素达到,这样可得迭代公式 dp[row][col]=dp[row-1][col]+dp[row][col-1]
4. 遍历完成后 dp矩阵存放了从其实位置到当前位置的所有可能走法,因此返回dp[m-1][n-1]就是需要的值
leetcode 【 Unique Paths 】python 实现的更多相关文章
- [leetcode]Unique Paths @ Python
原题地址:https://oj.leetcode.com/problems/unique-paths/ 题意: A robot is located at the top-left corner of ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
- [leetcode]Unique Paths II @ Python
原题地址:https://oj.leetcode.com/problems/unique-paths-ii/ 题意: Follow up for "Unique Paths": N ...
- [LeetCode] Unique Paths II 不同的路径之二
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [LeetCode] Unique Paths 不同的路径
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- LeetCode: Unique Paths I & II & Minimum Path Sum
Title: https://leetcode.com/problems/unique-paths/ A robot is located at the top-left corner of a m ...
- [LeetCode] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )
Unique Paths https://oj.leetcode.com/problems/unique-paths/ A robot is located at the top-left corne ...
- Leetcode Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]
唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...
- Leetcode Unique Paths
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
随机推荐
- Google pieCharts的学习
在公司项目开发过程中, 尤其是在网站的开发过程中,用到很多的前端的插件,在这里, 我简单介绍下近期Google pieCharts的是使用方法 https://developers.google.co ...
- zabbix-3.4-快速入门
1 登陆和配置用户 登陆Zabbix,以及在Zabbix内建立一个系统用户. 用户名:Admin 或者 admin 密码 : zabbix 增加用户 可以在 管理(Administration) → ...
- 本地eclipse启动tomcat后无法访问
转自博文:http://blog.csdn.net/wqjsir/article/details/7169838/ 症状: tomcat在eclipse里面能正常启动,而在浏览器中访问http://l ...
- MVC 控制器向View传值的三种方法(转)
1.提供视图模型对象 你能把一个对象作为View方法的参数传递给视图. public ViewResult Index() { DateTime date = DateTime.Now; return ...
- 无法通过CTRL+空格及SHIFT+CTRL调出输入法的解决方案
打开任务管理器: 运行:CTFMON.EXE
- linux 命令——38 cal (转)
cal命令可以用来显示公历(阳历)日历.公历是现在国际通用的历法,又称格列历,通称阳历.“阳历”又名“太阳历”,系以地球绕行太阳一周为一年,为西方各国所通用,故又名“西历”. 1.命令格式: cal ...
- idea搭建ssm
第一步:打开intellij idea,创建maven项目 参考:http://blog.csdn.net/w8897282/article/details/71173211 1.创建一个maven ...
- 根据图片的URL来实例化图片
正常的Image图片类实例化的时候都需要使用本地的虚拟路径而不能使用URL,如果使用URL就会出现 不支持 URI 格式 这样的问题,正确的写法如下: HttpWebRequest reques ...
- BZOJ 4128: Matrix
BZOJ 4128: Matrix 标签(空格分隔): OI BZOJ 大步小步 矩阵 费马小定理 Time Limit: 10 Sec Memory Limit: 128 MB Descriptio ...
- 管道命令'|' 和xargs find命令找到后把所有找到的删除
管道符号,是unix功能强大的一个地方,符号是一条竖线:"|", 用法: command 1 | command 2 他的功能是把第一个命令command 1执行的结果作为comm ...