天天看點

[LeetCode]--63. Unique Paths II

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.

尋求最短路徑,從左上走到右下,保證每次隻能往左走或往下走(不可以斜着走)。其中數字1是障礙,表示“此路不通”,求總共的路線數。

第一種二維數組

用一個二維數組來表示前者的路徑

核心就是這個,如果不等于1,我們就找到前者的路徑相加。

第二種一維數組

其實一維數組足以表示前者的路徑,因為一維數組左邊是你更新過的,右邊是沒更新,沒更新的相當于上一排,也就是上一排的來路加上左邊的來路之和就是現在的來路。(解釋好混亂,但我是這樣想就了解了)