首页 > 其他分享 >LeetCode 62. 不同路径(/)

LeetCode 62. 不同路径(/)

时间:2023-02-25 01:22:27浏览次数:37  
标签:uniquePaths int 路径 Solution class ++ 62 ans LeetCode

题目

约束

题解

解法一

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

解法二


class Solution {
public:
    int uniquePaths(int m, int n) {
        long long ans = 1;
        for (int x = n, y = 1; y < m; ++x, ++y) {
            ans = ans * x / y;
        }
        return ans;
    }
};

标签:uniquePaths,int,路径,Solution,class,++,62,ans,LeetCode
From: https://www.cnblogs.com/chuixulvcao/p/17153647.html

相关文章