LeetCode. Different Path

Topics requirements:

A robot located in a left corner mxn grid (starting point figure below labeled "Start").

The robot can only move one step to the right or down. Robot trying to reach the bottom right corner of the grid (in the following figure labeled "Finish").

Q. How many different paths there are in total?

Example:

Input: m = 3, n = 2 Output: 3 Explanation: From top left corner, a total of three paths to the lower right corner.

  1. Right right → down →
  2. Right → down to right →
  3. Right right → down →

Code:

class Solution {
public:
    int uniquePaths(int m, int n) {
        vector<vector<int>> vec(m, vector<int>(n, 1));
        for(int i = 1; i < m; i++) {
            for(int j = 1; j < n; j++) {
                vec[i][j] = vec[i-1][j] + vec[i][j-1];
            }
        }
        return vec[m-1][n-1];
    }
};

Guess you like

Origin www.cnblogs.com/leyang2019/p/11696110.html