Skip to content

Latest commit

 

History

History
25 lines (24 loc) · 690 Bytes

63不同路径II.md

File metadata and controls

25 lines (24 loc) · 690 Bytes
func uniquePathsWithObstacles(obstacleGrid [][]int) int {
    // dp[i][j]为到达ij处的路径数
    m, n := len(obstacleGrid), len(obstacleGrid[0])
    dp := make([][]int, m)
    for i := range dp {
        dp[i] = make([]int, n)
    }
    for i := 0; i < m && obstacleGrid[i][0] != 1; i++ {
        dp[i][0] = 1
    }
    for j := 0; j < n && obstacleGrid[0][j] != 1; j++ {
        dp[0][j] = 1
    }
    for i := 1; i < m; i++ {
        for j := 1; j < n; j++ {
            if obstacleGrid[i][j] != 1 { // 只有当没有障碍时才需要记录路径数
                dp[i][j] = dp[i-1][j] + dp[i][j-1]
            }
            
        }
    }
    return dp[m-1][n-1]
}