Leetcode - 62. Path problem_different paths

topic link

 A robot is located in the upper left corner of an mxn grid (the starting point is marked "Start" in the figure below).

 The robot can only move one step down or to the right at a time. The robot tries to reach the bottom right corner of the grid (marked "Finish" in the image below).

 How many different paths are there in total?

Example 1:

Here is the quote

Example 2:

Input: m = 3, n = 2
Output: 3

Explanation:
Starting from the upper left corner, there are a total of 3 paths to reach the lower right corner.
 ①Right -> Down -> Down
 ②Down -> Down -> Right
 ③Down -> Right -> Down

untie:

insert image description here

code:

class Solution {
    
    
public:
    int uniquePaths(int m, int n) {
    
    
        
        //创建dp数组
        vector<vector<int>> dp(m+1,vector<int>(n+1));
        //初始化
        dp[0][1]=1;

        //填dp表
        for(int i=1;i<=m;i++)//从上往下遍历每一行
        {
    
    
            for(int j=1;j<=n;j++)//从左往右遍历每个元素
            {
    
    
                //填表
                dp[i][j]=dp[i-1][j]+dp[i][j-1]; 
            }
        }
        //返回值
        return dp[m][n];
    }
};

insert image description here

Guess you like

Origin blog.csdn.net/Tianzhenchuan/article/details/131625325