每日一题 2019 - 04 - 06
题目:
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).
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.
Note: m and n will be at most 100.
Example 1:
1 | Input: |
解法:
这个题比 leetcode 62 多了一个限制条件就是,地图中可能存在不能通行的障碍物,遇到障碍物需要躲避,那么在这种约束条件下,我们只需要排除异样即可,思路大致如下:
- 初始化
dp
地图,dp
第一行1
位置后不通行,所以1
后所有可走的情况为0
;dp
第一列1
位置后不通行,所以1
后所有可走的情况为0
- 递推公式依然是 :
path[j][i] = path[j - 1][i] + path[j][i - 1];
- 需要加上限制条件,如果非第一行第一列的其他位置出现
1
,则该位置的可走的路数的总和为0
代码:
1 | class Solution { |