关闭

101. Unique Paths

87人阅读 评论(0) 收藏 举报
分类:

-62. Unique Paths My Submissions QuestionEditorial Solution
Total Accepted: 89668 Total Submissions: 244895 Difficulty: Medium
A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below).

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish’ in the diagram below).

How many possible unique paths are there?

Above is a 3 x 7 grid. How many possible unique paths are there?

Note: m and n will be at most 100.

难度:简单!!!
思路: 三种情况:在两个边界和在中间时
写出递推公式,自底向上便可求出

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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:766642次
    • 积分:9560
    • 等级:
    • 排名:第1933名
    • 原创:197篇
    • 转载:157篇
    • 译文:2篇
    • 评论:12条
    最新评论