您的位置:首页 > 产品设计 > UI/UE

[LeetCode]63 Unique Paths II

2015-01-04 09:16 357 查看
https://oj.leetcode.com/problems/unique-paths-ii/
http://blog.csdn.net/linhuanmars/article/details/22135231
public class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {

// Validations
if (obstacleGrid == null || obstacleGrid.length == 0 || obstacleGrid[0].length == 0)
return 0;   // Invalid input

int m = obstacleGrid.length;
int n = obstacleGrid[0].length;

int[][] paths = new int[m]
;
for (int i = 0 ; i < m ; i ++)
{
for (int j = 0 ; j < n ; j ++)
{
int v = 0;
if (obstacleGrid[i][j] == 1)
{
// obstacle
v = 0;
}
else if (i == 0 && j == 0)
{
// Start point
v = 1;
}
else
{
int left = j > 0 ? paths[i][j - 1] : 0;
int up = i > 0 ? paths[i - 1][j] : 0;
v = left + up;
}
paths[i][j] = v;
}
}
return paths[m - 1][n - 1];
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  LeetCode