62. Unique Paths i & ii

62. 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 reach the bottom-right corner of the grid (marked ‘Finish‘ in the diagram below).

How many possible unique paths are there?

DP  注意边界

public class Solution {
    public int uniquePaths(int m, int n) {
        int dp[][] = new int[n][m];
        for(int i = 0 ; i < n ; i++){
          dp[i][0] = 1;
        }
        for(int j = 0 ; j < m ; j ++){
          dp[0][j] = 1;
        }
        for(int i = 1; i < n ; i++){
            for(int j = 1 ; j < m ; j++){
                dp[i][j] = dp[i-1][j] + dp[i][j-1];
            }
        }
        return dp[n-1][m-1];
    }
}

63. 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 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.

public class Solution {
    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        if(obstacleGrid == null || obstacleGrid.length == 0 || obstacleGrid[0][0] == 1) return 0;
        int[][] dp = new int[obstacleGrid.length][obstacleGrid[0].length] ;
        for(int i = 0 ; i < obstacleGrid.length; i++){
            if(obstacleGrid[i][0] != 1)
                dp[i][0] = 1;
            else
               break;
        }

        for(int i = 0 ; i < obstacleGrid[0].length; i++){
            if(obstacleGrid[0][i] != 1)
                dp[0][i] = 1;
            else
               break;
        }

        for(int i = 1; i < obstacleGrid.length ; i++){
            for(int j = 1; j < obstacleGrid[0].length ; j++){
                if(obstacleGrid[i][j] == 1)
                    dp[i][j] = 0;
                if(obstacleGrid[i][j] == 0)
                   dp[i][j] = dp[i-1][j] + dp[i][j-1];
            }
        }

        return dp[obstacleGrid.length-1][obstacleGrid[0].length -1];
    }
}
时间: 2024-10-16 10:21:08

62. Unique Paths i & ii的相关文章

LeetCode 62, 63 Unique Paths I+II

62. Unique Paths 空间可以按行优化为 O(n),也可以按列优化为O(m). class Solution { public: int uniquePaths(int m, int n) { int dp[m][n]={0}; dp[0][0]=1; for (int i=0;i<m;++i){ for (int j=0;j<n;++j){ if (i==0&&j==0) continue; dp[i][j]=0; if (j-1>=0) dp[i][j]

LeetCode --- 62. Unique Paths

题目链接: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 reach the bottom-right corner of the grid (ma

[Leetcode]@python 62. Unique Paths

题目链接:https://leetcode.com/problems/unique-paths/ 题目大意:给定n.m,在mxn的矩阵中,从(0,0)走到(m-1,n-1)一共有多少种法(只能往下和往右走) 解题思路:从(0,0)到(m-1,n-1)一共要走m - 1次向下,n-1次向右.也就是在n + m - 2次中选出m-1次向下,也就是C(m + n - 2,m-1) class Solution(object): def uniquePaths(self, m, n): ""&

刷题62. Unique Paths

一.题目说明 题目62. Unique Paths,在一个m*n矩阵中,求从左上角Start到右下角Finish所有路径.其中每次只能向下.向右移动.难度是Medium! 二.我的解答 这个题目读读题目,理解后不难.定义一个dp[m][n],初始化最后一列为1,最后一行为1,然后循环计算到dp[0][0]就可以了. 代码如下: class Solution{ public: int uniquePaths(int m,int n){ vector<vector<int>> dp(m

Unique Paths I&amp;&amp;II

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 reach the bottom-right corner of the grid (marked

Unique Paths I,II

题目来自于:https://leetcode.com/problems/unique-paths/ :https://leetcode.com/problems/unique-paths-ii/ 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 poi

LeetCode: Unique Paths I &amp; II

Title: 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 'Finis

62.Unique Paths (法1递归-动态规划法2数学公式)

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. Therobot is trying to reach the bottom-right corner of the grid (marked 'Finish'in the

62. Unique Paths

题目: A robot is located at the top-left corner of a m x ngrid (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 'Finish' i