您的位置:首页 > 其它

leetcode-62-不同路径

2019-09-08 11:14 316 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/ChenD17/article/details/100622920

//dp,AC

class Solution {
public:
    int uniquePaths(int m, int n) {
        vector<int> row(m, 1);
        vector<vector<int>> dp(n, row);
        for (int i = 1; i < n; i++) {
            for (int j = 1; j < m; j++) {
                dp[i][j] = dp[i][j - 1] + dp[i - 1][j];
            }
        }
        return dp[n - 1][m - 1];
    }
};
int main() {
    Solution sol;
    int n = 2, m=3;
    cout << sol.uniquePaths(n, m) << endl;
    return 0;
}

 

//递归,未AC,超时

class Solution {
public:
    int helper(vector<int> point, vector<int> target, long long int &time) {
        if (point == target) time++;
        if (point[0] < target[0]) {
            //point[0]++;
            helper({ point[0] + 1, point[1] }, target, time);
        }
        if (point[1] < target[1]) {
            //point[1]++;
            helper({point[0], point[1]+1}, target, time);
        }
        return 0;
    }
    int uniquePaths(int m, int n) {
        long long int time=0;
        helper({ 1,1 }, { m,n }, time);
        return time;
    }
};

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: