Unique Paths II leetcode java

题目

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.

题解:

这道题大体想法跟Unique Path是一样的。

只是要单独考虑下障碍物对整个棋盘的影响。

先看看初始条件会不会受到障碍物的影响。

假设整个棋盘只有一行,那么在第i个位置上设置一个障碍物后,说明位置i到最后一个格子这些路都没法走了。

如果整个棋盘只有一列,那么第i位置上的障碍物,也会影响从第i位置往后的路。

所以说明,在初始条件时,如果一旦遇到障碍物,障碍物后面所有格子的走法都是0。

再看求解过程,当然按照上一题的分析dp[i][j] = dp[i-1][j] + dp[i][j-1] 的递推式依然成立(机器人只能向下或者向右走嘛)。但是,一旦碰到了障碍物,那么这时的到这里的走法应该设为0,因为机器人只能向下走或者向右走,所以到这个点就无法通过。

处理完障碍物的特殊问题,依照unique paths改一下代码就好。

代码如下:

1    public int uniquePathsWithObstacles(int[][] obstacleGrid) {  
 2         int m = obstacleGrid.length;  
 3         int n = obstacleGrid[0].length;  
 4         
 5         if(m==0||n == 0)  
 6             return 0; 
 7         
 8         if(obstacleGrid[0][0] == 1 || obstacleGrid[m-1][n-1] == 1)  
 9             return 0; 
10             
11         int[][] dp = new int[m][n]; 
12         
13         dp[0][0] = 1;  
14         for(int i = 1; i < n; i++){  
15             if(obstacleGrid[0][i] == 1)  
16                 dp[0][i] = 0;  
17             else 
18                 dp[0][i] = dp[0][i-1];  
19         }  
20         
21         for(int i = 1; i < m; i++){  
22             if(obstacleGrid[i][0] == 1)  
23                 dp[i][0] = 0;  
24             else 
25                 dp[i][0] = dp[i-1][0];  
26         }  
27         
28         for(int i = 1; i < m; i++){  
29             for(int j = 1; j < n; j++){  
30                 if(obstacleGrid[i][j] == 1)  
31                     dp[i][j] = 0;  
32                 else  
33                     dp[i][j] = dp[i][j-1] + dp[i-1][j];  
34             }  
35         }  
36         return dp[m-1][n-1];  
37     }

Unique Paths II leetcode java

时间: 2024-11-07 07:48:56

Unique Paths II leetcode java的相关文章

Unique Paths II -- leetcode

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 middl

LeetCode 63 _ Unique Paths II 全部不同路径2

Description: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked

&lt;LeetCode OJ&gt; 63. Unique Paths II

63. Unique Paths II My Submissions Question Total Accepted: 55136 Total Submissions: 191949 Difficulty: Medium Follow up for "Unique Paths":紧接着上一题"唯一路劲",现在考虑有一些障碍在网格中,无法到达,请重新计算到达目的地的路线数目 Now consider if some obstacles are added to the

LeetCode --- 63. Unique Paths II

题目链接:Unique Paths II 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

[LeetCode] Unique Paths &amp;&amp; Unique Paths II &amp;&amp; Minimum Path Sum (动态规划之 Matrix DP )

Unique Paths https://oj.leetcode.com/problems/unique-paths/ A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to rea

[LeetCode][JavaScript]Unique Paths II

Unique Paths II 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 obsta

【Leetcode】Unique Paths II

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 middl

Unique Binary Search Trees II leetcode java

题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example, Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 题解:这道题比1难的就是不是返回个数,而

LeetCode: Unique Paths II 解题报告

Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution 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 spac