63. Unique Paths II
来源:互联网 发布:网络维保服务模式 编辑:程序博客网 时间:2024/05/20 10:52
Follow up for "Unique Paths":
Now consider if some obstacles are added to the grids. How many unique paths would there be?
An obstacle and empty space is marked as 1
and 0
respectively in the grid.
For example,
There is one obstacle in the middle of a 3x3 grid as illustrated below.
[ [0,0,0], [0,1,0], [0,0,0]]
The total number of unique paths is 2
.
Note: m and n will be at most 100.
思路:将两边和1初始化即可。
class Solution: def uniquePathsWithObstacles(self, obstacleGrid): """ :type obstacleGrid: List[List[int]] :rtype: int """ if obstacleGrid[-1][-1]==1 or obstacleGrid[0][0]==1: return 0 m,n=len(obstacleGrid),len(obstacleGrid[0]) dp=[[0]*n for i in range(m)] for j in range(n): if obstacleGrid[0][j]==0: dp[0][j]=1 else: for jj in range(j,n): dp[0][jj]=0 break for i in range(m): if obstacleGrid[i][0]==0: dp[i][0]=1 else: for ii in range(i,m): dp[ii][0]=0 break for i in range(1,m): for j in range(1,n): dp[i][j]=dp[i-1][j]+dp[i][j-1] if obstacleGrid[i][j]!=1 else 0 return dp[-1][-1]
阅读全文
0 0
- 63. Unique Paths II
- 63. Unique Paths II
- 63. Unique Paths II
- 63. Unique Paths II
- 63. Unique Paths II
- 63. Unique Paths II
- 63. Unique Paths II
- 63. Unique Paths II
- 63. Unique Paths II
- 63. Unique Paths II
- 63. Unique Paths II
- 63. Unique Paths II
- 63. Unique Paths II
- 63. Unique Paths II
- 63. Unique Paths II
- 63. Unique Paths II
- 63. Unique Paths II
- 63. Unique Paths II
- Java--异常处理
- C# 代码如何实现让你的电脑关机,重启,注销,锁定,休眠,睡眠
- ADS开发环境的使用
- 独立安装WAMP环境 与众不同如此轻松
- 一个子查询引发的血案
- 63. Unique Paths II
- Spring实战-读书笔记(七)-Spring MVC的高级技术
- struts2-13访问或添加几个属性(request/session/application属性)
- mysql查询Lost connection to MySQL server during query
- ActiveMQ安装启动
- Zxing实现二维码生成和解析,可带logo
- Java内部类
- Self Dividing Numbers
- 关于Android源码中|= (m & STATIC_VALUE) !=0 的理解