Follow up for "Unique Paths":
Now consider if some obstacles are added to the grids. How many unique paths would there be?
An obstacle and empty space is marked as 1
and 0
respectively in the grid.
For example,
There is one obstacle in the middle of a 3x3 grid as illustrated below.
[ [0,0,0], [0,1,0], [0,0,0] ]
The total number of unique paths is 2
.
Note: m and n will be at most 100.
public class Solution { public int uniquePathsWithObstacles(int[][] obstacleGrid) { int m = obstacleGrid.length; int n = obstacleGrid[0].length; if (m <= 0 || n <= 0) { return 0; } if(obstacleGrid[0][0] == 1 || obstacleGrid[m-1][n-1] == 1) return 0; int[][] res = new int[m][n]; res[0][0] = 1; for (int i = 1; i < m; i++) { if (obstacleGrid[i][0] == 1) { res[i][0] = 0; } else { res[i][0] = res[i-1][0]; } } for (int i = 1; i < n; i++) { if (obstacleGrid[0][i] == 1) { res[0][i] = 0; } else { res[0][i] = res[0][i-1]; } } for (int i = 1; i < m; i++) { for (int j = 1; j < n; j++) { if (obstacleGrid[i][j] == 1) { res[i][j] = 0; } else { res[i][j] = res[i-1][j] + res[i][j-1]; } } } return res[m-1][n-1]; } }
相关推荐
javascript js_leetcode题解之63-unique-paths-ii.js
c语言入门 C语言_leetcode题解之63-unique-paths-ii.c
Unique Paths Unique Paths II Longest Palindromic Substring Interleaving String Triangle Distinct Subsequences Decode Ways Palindrome Partitioning II Maximal Rectangle ###Recursion N-Queens N-Queens II...
44. Unique Paths II:与 Unique Paths 类似,但是考虑网格中障碍物的处理。 45. Maximum Sum Subarray:找出一个数字序列中和最大的连续子序列。 46. Maximum Product Subarray:找出一个数字序列中乘积最大的连续...
javascript js_leetcode题解之62-unique-paths.js
44. Unique Paths II:不同路径数的计算,其中包含障碍物。 45. Maximum Sum Subarray:计算子数组的最大和。 46. Maximum Product Subarray:计算子数组的最大乘积。 47. Coins in a Line:动态规划解决博弈问题,...
- "不同路径"(Unique Paths)和"不同路径 II"(Unique Paths II)同样是关于路径计算的动态规划问题。 **二分搜索(Binary Search)** 二分搜索算法适用于有序数组或列表的快速查找问题。 - "搜索插入位置"...
c语言入门 C语言_leetcode题解之62-unique-paths.c
62.Unique Paths, 63.Unique Paths II, 64.Minimum Path Sum 2017/03/09 70.Climbing Stairs, 198.House Robber, 55.Jump Game 72.Edit Distance, 97.Interleaving String, 115.Distinct Subsequences 2017/04/24 ...
- Unique Paths / Unique Paths II: 前者计算从矩阵的左上角到右下角的路径数量;后者则考虑了障碍物。 - Minimum Path Sum: 在一个m×n的网格中,找到从左上角到右下角的路径,使得路径上的数字总和最小。 - Plus ...
Unique Paths II **知识点:** - **问题描述:** - 同 62 题,但网格中存在障碍物。 - **解决方案分析:** - **动态规划:** - 定义 `dp[i][j]` 表示到达 `(i, j)` 的路径数量。 - 如果当前位置有障碍物,则...
1、记忆化 2、二维dp 3、滚动优化
II mod之后,可能数学公式就不能简单地给出答案了。但对我来说,其实和前一题没区别。动态规划处理这种问题,早就是牛刀杀鸡了。。 Single Number 碰巧我知道异或的解法。如果不知道的话,想想还是有点费事的。 ...
63.Unique Paths II 64.最小路径和 70.爬楼梯 72.编辑距离 122. 买卖股票的最佳时机 II 152.最大积子阵列 198.强盗 322.硬币变化 贪心算法 45.跳跃游戏II 55.跳跃游戏 哈希 1.二和 3.最长不重复字符的子串 49.组字谜...
【描述】"leetcode63" 提到的这个问题,全称为“Unique Paths II”,是LeetCode中的一个经典问题。在这个问题中,任务是计算一个机器人从左上角到达右下角有多少种不同的路径。与标准的“Unique Paths”问题相比,...
- **Probe Instrumentation Phase:** In this phase, a unique path ID is assigned to each path. This allows for the identification and tracking of individual paths during program execution. - **Backwalk ...
unique_paths.py(类似于 chess_board.c!)+ unique_paths2.py chess_board.c 效率测试 bin_search.py 素数.py 零知识.py Leetcode:现在很多小方案都是leetcode,我主要是为了好玩。 著名算法:Knuth-Morris-Pratt...
public static int uniquePaths(int rows, int cols, int[][] grid) { int[][] dp = new int[rows][cols]; // 初始化第一列全部为1,表示只有一条路径到达每个点 for (int i = 0; i ; i++) { if (grid[i][0] =...