跳至主要內容

063. 不同路径 II

T4mako算法数组动态规划矩阵小于 1 分钟

063. 不同路径 II

中等

题目描述open in new window

将障碍物填充 0

class Solution {
    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        int n = obstacleGrid.length, m = obstacleGrid[0].length;
        int[] f = new int[m];
        f[0] = obstacleGrid[0][0] == 0 ? 1 : 0;
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                if (obstacleGrid[i][j] == 1) {
                    f[j] = 0;
                    continue;
                }
                if (j - 1 >= 0 && obstacleGrid[i][j - 1] == 0) {
                    f[j] += f[j - 1];
                }
            }
        }
        return f[m - 1];
    }
}
评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v2.15.5