首页 > 代码库 > [LeetCode]63 Unique Paths II
[LeetCode]63 Unique Paths II
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][n]; 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]63 Unique Paths II
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。