LeetCode OJ 系列之63 Unique Paths II --Python
来源:互联网 发布:qq mac版手机 编辑:程序博客网 时间:2024/06/12 01:07
Problem:
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.
Answer:class Solution(object): def uniquePathsWithObstacles(self, obstacleGrid): """ :type obstacleGrid: List[List[int]] :rtype: int """ if obstacleGrid[-1][-1]==1:return 0 for i in range(len(obstacleGrid)): for j in range(len(obstacleGrid[i])): if obstacleGrid[i][j]==1: obstacleGrid[i][j]=-1 elif i==0 and j==0: obstacleGrid[i][j]=1 elif i==0: if obstacleGrid[i][j-1]==-1 or obstacleGrid[i][j-1]==0:obstacleGrid[i][j]=0 else : obstacleGrid[i][j]=1 elif j==0: if obstacleGrid[i-1][j]==-1 or obstacleGrid[i-1][j]==0:obstacleGrid[i][j]=0 else : obstacleGrid[i][j]=1 else: if obstacleGrid[i][j-1]==-1 and obstacleGrid[i-1][j]==-1:obstacleGrid[i][j]=0 elif obstacleGrid[i][j-1]==-1:obstacleGrid[i][j]=obstacleGrid[i-1][j] elif obstacleGrid[i-1][j]==-1:obstacleGrid[i][j]=obstacleGrid[i][j-1] else :obstacleGrid[i][j]=obstacleGrid[i-1][j]+obstacleGrid[i][j-1] return obstacleGrid[-1][-1]
0 0
- LeetCode OJ 系列之63 Unique Paths II --Python
- LeetCode OJ 系列之62 Unique Paths --Python
- LeetCode OJ:Unique Paths II
- LeetCode OJ - Unique Paths II
- LeetCode OJ Unique Paths II
- 【Leetcode】【python】Unique Paths/Unique Paths II
- leetCode系列----Unique Paths II
- LeetCode OJ 之 Unique Paths II(不同路径 - 二)
- leetcode 之Unique Paths II
- leetcode之Unique Paths II
- 【Leetcode】之Unique Paths II
- Leetcode之Unique Paths II
- leetcode oj java Unique Paths II
- LeetCode 63: Unique Paths II
- [leetcode 63] Unique Paths II
- [LeetCode 63]Unique Paths II
- leetcode || 63、Unique Paths II
- Unique Paths II - LeetCode 63
- 一些系统漏洞
- 打包输出文件太大
- 数据结构 — 树 与 二叉树、森林
- 关于“一秒一G LiFi比WiFi快一百倍”的评论
- 项目适配iOS9遇到的一些问题及解决办法
- LeetCode OJ 系列之63 Unique Paths II --Python
- iOS开发--UIApplication(IconBadgeNum)
- VS2008中的多个CMFCToolBar,在同一行显示!
- 【Android即时通讯】Android 高仿微信的即时聊天DEMO
- Android代码混淆、第三方平台加固加密、渠道分发 完整教程
- 2015.11.27-------KNN算法的python实现的应用
- Linux内核驱动之DDR3(一)寻址
- ios9控件新宠:UIStackView
- Swift 闭包