[LeetCode]Unique Paths II
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |
Question
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.
The total number of unique paths is 2.
Note: m and n will be at most 100.
本题难度Medium。
一维数组
【复杂度】
时间 O(N^2) 空间 O(N)
【思路】
与Unique Paths的一维数组法一样,只不过多了一个障碍,因此对于j!=0
的dp[j]
赋值:
但是要注意的情况是dp[0]
的赋值,比如:
第二行第一列的值为1,那么不仅它的本行dp[0]=0
,而且对于第三行第一列dp[0]=0
。所以在此,对于dp[0]
值的赋值为:
【代码】
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |