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 7 x 3 grid. How many possible unique paths are there?

Note: m and n will be at most 100.

Input: m = 3, n = 2
Output: 3
Explanation:
From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
1. Right -> Right -> Down
2. Right -> Down -> Right
3. Down -> Right -> Right

题目

给定MxN棋盘,只允许从左上往右下走,每次走一格。共有多少种走法?

思路

Matrix DP(二维DP) 问题

1. 初始化

预处理第一个row: dp[0][j] = 1  因为从左上起点出发,往右走的每一个unique path都是1

预处理第一个col:   dp[i][0]= 1  因为从左上起点出发,往下走的每一个unique path 都是1

2. 转移方程

因为要求所有possible unique paths之和

dp[i][j] 要么来自dp[i-1][j] 要么来自dp[i][j-1]

代码

 class Solution {
public int uniquePaths(int m, int n) {
int[][]dp = new int[n][m]; // [row][col] // 预处理第一个col
for(int i= 0; i < n; i++){
dp[i][0] = 1;
}
//预处理第一个row
for(int j=0; i < m; i++){
dp[0][j] = 1;
}
for(int i= 1; i < n; i++){
for(int j= 1; j < m; j++){
dp[i][j] = dp[i-1][j] + dp[i][j-1];
}
}
return dp[n-1][m-1];
}
}

[leetcode]62. Unique Paths 不同路径的更多相关文章

  1. [LeetCode] 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 ...

  2. LeetCode 62. Unique Paths不同路径 (C++/Java)

    题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...

  3. leetcode 62. Unique Paths 、63. Unique Paths II

    62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...

  4. [LeetCode] 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 ...

  5. LeetCode 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 ...

  6. [leetcode] 62 Unique Paths (Medium)

    原题链接 字母题 : unique paths Ⅱ 思路: dp[i][j]保存走到第i,j格共有几种走法. 因为只能走→或者↓,所以边界条件dp[0][j]+=dp[0][j-1] 同时容易得出递推 ...

  7. LeetCode: 62. Unique Paths(Medium)

    1. 原题链接 https://leetcode.com/problems/unique-paths/description/ 2. 题目要求 给定一个m*n的棋盘,从左上角的格子开始移动,每次只能向 ...

  8. 62. Unique Paths不同路径

    网址:https://leetcode.com/problems/unique-paths/ 第一思路是动态规划 通过观察,每一个格子的路线数等于相邻的左方格子的路线数加上上方格子的路线数 于是我们就 ...

  9. LeetCode 63. Unique Paths II不同路径 II (C++/Java)

    题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...

随机推荐

  1. bootstrap modal 移除数据

    有时业务需求,在弹出模态框时需要动态的展示,但是不做处理时,每次弹出的都是第一次的数据,所以需要监听模态框关闭,每次关闭需要先清除里面的数据. //监听弹页关闭 $("#myModal&qu ...

  2. Spring生态研习【三】:Spring-kafka

    1. 基本信息介绍 基于spring的kafka应用,非常简单即可搭建起来,前提是要有一个kafka的broker集群.我在之前的博文里面已经介绍并搭建了一套broker环境,参考Kafka研究[一] ...

  3. PHP-ML机器学习库之安装篇

    1.PHP-ML库安装要求:PHP>=7.1 2.切换到项目的跟目录下,使用composer进行安装:composer require php-ai/php-ml 安装完成后的目录如下: 新建测 ...

  4. 【C++】读取参数的类

    在C++程序中,如果我们把程序中的参数都保存在txt文本中,运行时再去读取.这样的好处是,当我们需要调参的时候,不需要每次都重新编译程序,大大提升了效率. 今日分享一份实现以上功能的代码,代码来源:h ...

  5. 使用nrm工具高效地管理npm源

    在使用npm时,官方的源下载npm包会比较慢,国内我们基本使用淘宝的源,如果公司内部搭建了一套npm私有仓库,公司内部的源不可能把npm官方的npm包都同步,所以需要切换npm源.如果使用npm/cn ...

  6. vue1 & vue2 数据驱动更新视图机制对比

    vue1 小粒度更新,精确追踪到数据变化所影响的dom变化,精确更新变化的dom 具体实现为,维护 observer watcher directive 三个类 ·observer负责监听数据变化,并 ...

  7. Oracle中,如何将String插入到BLOB类型字段

    1,String插入到BLOB类型字段,(这里的字符串以生成的XML为例): String XML = document.asXML();  //使用dom4j写成的xml是String类型,记得st ...

  8. 使用Jenkins部署Python项目

    廖大使用Fabric部署的.我使用Jenkins试试部署过程.虽然说是用python项目部署测试的,但其他项目也是同理的. 参考Jenkins+Python部署完整版,不过安装方式不同. 安装tomc ...

  9. iReport-5.6.0 新建文件为什么是灰色的?新建项目没有选择项?

    从网上下了绿色版和安装版都出现这个问题. 解决:发现原来是有些插件没有激活,进入手动激活就ok了 -->工具-->插件-->已安装 ,选择未激活的手动激活. 激活成功后如下图(和我同 ...

  10. 解决spring-security session超时 Ajax 请求没有重定向的问题

    开始时, 代码是这样的: $.ajax({ type : "POST", url : sSource, cache : false, dataType : "json&q ...