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. Python开发 基礎知識 3.類別&方法 (bool & str) (未完待續)

    類別 可使用type()查看 內建 [ 布爾:bool (Boolen) 字串:str (String) 數字:int (Integer) 小數:float 列表:list 元祖:tuple 字典:d ...

  2. JVM的基本结构及其各部分详解(二)

    3.2 栈帧组成之操作数栈 操作数栈是栈帧的主要内容之一,它主要用于保存计算过程中的中间结果,同时作为计算过程中变量临时的存储空间. 操作数栈也是一个先进后出的数据结构,只支持入栈和出栈两种操作,许多 ...

  3. Dockerfile之nginx(六)

    一.Dokcerfile的基本指令   1)From 指定构建镜像的基础镜像 2)MAINTAINER 指定镜像的作者 3)RUN 使用前一条指令创建的镜像生产容器,并在容器中执行命令,执行结束后会自 ...

  4. python 常用的模块

    面试的过程中经常被问到使用过那些python模块,然后我大脑就出现了一片空白各种模块一顿说,其实一点顺序也没有然后给面试官造成的印象就是自己是否真实的用到这些模块,所以总结下自己实际工作中常用的模块: ...

  5. Pyhon文件的用途

    Python1个文件2种用途: 当文件被当做脚本运行时 __name__='__main__'当模块被导入使用 __name__='spam' ==>等于模块名 假定spam代码如下 money ...

  6. C# 调用Tesseract实现OCR

    介绍 Tesseract是一个基于Apache2.0协议开源的跨平台ocr引擎,支持多种语言的识别,在Windows和Linux上都有良好的支持. 创建工程 创建一个C#的控制台工程 添加System ...

  7. C常量与控制语句

    在C语言中定义常量的两种方式 在C语言编程中定义常量有两种方法. const关键字 #define预处理器 1. const关键字 const关键字用于定义C语言编程中的常量. const float ...

  8. QQ第三方登录(完结篇)

    书接上回,上回说到:这篇是代码篇 首先我们先来看一下我的母鹿(目录)吧 Connect2.1  是我们从下载的SDK,内容包含 其他文件在配置之后全部删除了! index.html 是我们点击登陆的页 ...

  9. 调整iframe滚动条失效

    1:<iframe scrolling="auto" frameborder="0" src="' + add + '" style= ...

  10. Python正则替换字符串函数re.sub用法示例(1)

    本文实例讲述了Python正则替换字符串函数re.sub用法.分享给大家供大家参考,具体如下: python re.sub属于python正则的标准库,主要是的功能是用正则匹配要替换的字符串然后把它替 ...