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 ...
随机推荐
- Swagger的使用
参考文章: https://blog.csdn.net/xupeng874395012/article/details/68946676/ https://blog.csdn.net/hry2015 ...
- git剖析
GIT(分布式版本控制系统) Git是一款免费.开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目. 1.特点 分布式相比于集中式的最大区别在于开发者可以提交到本地,每个开发者通过克隆(g ...
- UOJ#210. 【UER #6】寻找罪犯 2-sat
#210. [UER #6]寻找罪犯 链接:http://uoj.ac/problem/210 想法:2-sat模型.每个人拆点,分别表示为犯人.非犯人.每个句供词拆点,分别表示真话.假话.供词与对应 ...
- linux 命令——3 pwd (转)
Linux中用 pwd 命令来查看”当前工作目录“的完整路径. 简单得说,每当你在终端进行操作时,你都会有一个当前工作目录. 在不太确定当前位置时,就会使用pwd来判定当前目录在文件系统内的确切位置. ...
- securetextentry 切换后有空格问题解决
搜索发现这个问题,网上的解决方法不明确,对于小白怎么办 直接上代码: - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundCo ...
- python_66_生成器2
import time def consumer(name): print('%s准备吃包子 '%name) while True: baozi=yield print('包子[%s]来了,被[%s] ...
- python_62_装饰器5
import time def timer(func): #timer(test1) func=test1 def deco(*args,**kwargs): start_time=time.time ...
- Debug与Release版本的区别详解
原文链接 Debug 和 Release 并没有本质的区别,他们只是VC预定义提供的两组编译选项的集合,编译器只是按照预定的选项行动.如果我们愿意,我们完全可以把Debug和Release的行为完全颠 ...
- sass安装更新及卸载方法
在 Windows 平台下安装 Ruby 需要先有 Ruby 安装包,大家可以到 Ruby 的官网(http://rubyinstaller.org/downloads)下载对应需要的 Ruby 版本 ...
- http请求中客户端真实的ip
private String getRemoteAddr() { String ip = ""; String unknow = "unknown"; try ...