【一天一道LeetCode】#62. Unique Paths

一天一道LeetCode系列

(一)题目

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?

Above is a 3 x 7 grid. How many possible unique paths are there?

Note: m and n will be at most 100.

(二)解题

主要思想:对于i,j这一点来说,它到终点的路径数dp[i][j] = dp[i+1][j]+ dp[i][j+1],这就是状态转移方程,然后利用动态规划来求解!

递归版本

class Solution {
public:
    int dp[101][101];//用来标记已经计算过的路径
    int uniquePaths(int m, int n) {
        dp[m-1][n-1] = 1;
        int ret = dfsPath(0,0,m-1,n-1);
        return ret;
    }
    int dfsPath(int pm,int pn,int m ,int n)
    {

        if(pm==m && pn==n) return 1 ;
        int down = 0;
        int right = 0;
        if(pm+1<=m) down = dp[pm+1][pn]==0?dfsPath(pm+1,pn,m,n):dp[pm+1][pn];//往下走的那一格到终点的路径数
        if(pn+1<=n) right = dp[pm][pn+1]==0?dfsPath(pm,pn+1,m,n):dp[pm][pn+1];//往右走的那一格到终点的路径数
        dp[pm][pn] = down+right;
        return dp[pm][pn];
    }
};

非递归版本

/*
提示:这个版本画个图可能会更好理解
*/
class Solution {
public:
    int uniquePaths(int m, int n) {
        int dp[101][101];
        for(int i = 0 ; i < m ; i++) dp[i][n-1] = 1;//首先初始化dp
        for(int i = 0 ; i < n ; i++) dp[m-1][i] = 1;
        if(m==1||n==1) return 1;//特殊情况
        for(int i = m-2 ; i>=0 ; i--)
            for(int j = n-2 ; j>=0 ; j--)
            {
                dp[i][j] = dp[i+1][j] + dp[i][j+1];//状态转移方程
            }
        return dp[0][0];
    }
};
时间: 2024-09-30 05:51:05

【一天一道LeetCode】#62. Unique Paths的相关文章

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 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 t

LeetCode 62. Unique Paths Java

题目: 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'

[LeetCode] 62.Unique Paths I

Unique Paths I 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 (marke

19.2.9 [LeetCode 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 t

[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): ""&

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

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 t

【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

62. Unique Paths i &amp; 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 (mar