[动态规划] leetcode 62 Unique Paths

problem:https://leetcode.com/problems/unique-paths/

         简单的dp题,类爬台阶题目。

class Solution {
    vector<vector<int>> dp;
public:
    int uniquePaths(int m, int n) {
        if (m == 0 || n == 0)return 0;
        dp.resize(n, vector<int>(m, 0));

        dp[0][0] = 1;
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                if (i != 0 || j != 0)
                {
                    int x = i >= 1 ? dp[i - 1][j] : 0;
                    int y = j >= 1 ? dp[i][j - 1] : 0;
                    dp[i][j] = x + y;
                }
            }
        }
        return dp[n - 1][m - 1];
    }
};

猜你喜欢

转载自www.cnblogs.com/fish1996/p/11318384.html