Leetcode#62. Unique Paths

题目描述: m * n 的矩阵, 从左上到右下,只能向下或者向右,一共有多少种走法;

解题思路:典型的动态规划问题

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

猜你喜欢

转载自blog.csdn.net/sinat_20177327/article/details/79967397