62. Unique Paths

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?


dummy pic

Above is a 3 x 7 grid. How many possible unique paths are there?
Note: m and n will be at most 100.


Fairly easy piece of cake. 最短路径 动态规划

class Solution {
    public int uniquePaths(int m, int n) {
        int[][] dp = new int[m][n];
        for(int i = 0;i<m;i++){
            dp[i][0]=1;
        }
        for(int j = 0;j<n;j++){
            dp[0][j]=1;
        }
        for(int i = 1; i<m;i++){
            for(int j = 1;j<n;j++){
                dp[i][j]=dp[i-1][j]+dp[i][j-1];
            }
        }
        /*
        for(int i = 0; i<m;i++){
            for(int j = 0;j<n;j++){
                System.out.print(dp[i][j]+", ");
            }
            System.out.println();
        }
        */
        return dp[m-1][n-1];
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • A robot is located at the top-left corner of a m x n grid...
    matrxyz阅读 184评论 0 0
  • A robot is located at the top-left corner of a m x n grid...
    灰睛眼蓝阅读 182评论 0 0
  • A robot is located at the top-left corner of a m x n grid...
    HalcyonMoon阅读 201评论 0 0
  • 风雨血腥 侠义道为何 谁人凭此情 了却英雄梦 失去是珍惜 得到又是累赘 忧困无处安放 活是倚栏思江湖
    文活阅读 192评论 1 1
  • 终于开始写作了,也终于找到个地方可以好好写作了。对于一个单身,无依无靠而未来遥不可及的人而言,这不啻是一种顶级的恩...
    纽约漂阅读 294评论 0 2

友情链接更多精彩内容