Unique Paths II
2016-12-29 19:55
113 查看
1.题目
"不同的路径" 的跟进问题:现在考虑网格中有障碍物,那样将会有多少条不同的路径?
网格中的障碍和空位置分别用 1 和 0 来表示。
m 和 n 均不超过100
如下所示在3x3的网格中有一个障碍物:
[ [0,0,0], [0,1,0], [0,0,0] ]
一共有2条不同的路径从左上角到右下角。
2.算法
每次我们要判断一下是不是障碍,如果是,则res[i][j]=0,否则还是res[i][j]=res[i-1][j]+res[i][j-1]。public int uniquePathsWithObstacles(int[][] obstacleGrid)
{
// write your code here
if (obstacleGrid == null || obstacleGrid.length == 0 || obstacleGrid[0].length == 0)
{
return 0;
}
int[] res = new int[obstacleGrid[0].length];
res[0] = 1;
for (int i = 0; i < obstacleGrid.length; i++)
{
for (int j = 0; j < obstacleGrid[0].length; j++)
{
if (obstacleGrid[i][j] == 1)
{
res[j] = 0;
}
else
{
if (j > 0)
{
res[j] += res[j-1];
}
}
}
}
return res[obstacleGrid[0].length - 1];
}
相关文章推荐
- unique-paths-ii
- 63. Unique Paths II
- LeetCode--Unique Paths II
- Leetcode Unique Paths II
- 63. Unique Paths II
- [Leetcode 63, Medium] Unique Paths II
- [LeetCode] Unique Paths II
- 63. Unique Paths II
- leetcode_question_63 Unique Paths II
- [Leetcode] Unique Paths II
- [LeetCode] Unique Paths II
- leetcode 63: Unique Paths II
- 63. Unique Paths II
- leetcode Unique Paths II
- LeetCode-63-Unique Paths II(C语言实现)
- 63. Unique Paths II
- Unique Paths I,II, Minimum Path Sum
- leetcode Unique Paths II
- LeetCode - Unique Paths II
- LeetCode | Unique Paths II