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. 虚拟机安装及Oracle安装

    1.安装虚拟机(没难度,傻瓜装机) 新建虚拟机 自定义------下一步------- 稍后安装操作系统------下一步 下一步 下一步至完成 然后启动,就可以启动一个系统咯!!! 可以查一下虚拟机 ...

  2. ILBC 运行时 (ILBC Runtime) 架构

    本文是 VMBC / D# 项目 的 系列文章, 有关 VMBC / D# , 见 <我发起并创立了一个 VMBC 的 子项目 D#>(以下简称 <D#>)  https:// ...

  3. jmeter配置脚本录制进行抓包并快速分析、定位接口问题

    对于测试人员.开发人员来说,善用抓包工具确实是快速分析和定位问题的一大必备神技,现将配置过程记录如下: 1.打开jmeter后,首先添加一个线程组: 2.线程组可以重新命名按项目名称分类 3.然后在工 ...

  4. springboot中添加热部署

    <dependency> <!--Spring 官方提供的热部署插件 --> <groupId>org.springframework.boot</group ...

  5. 批处理关闭防火墙.bat

    批处理关闭防火墙.bat @echo offecho 用批处理关闭防火墙,包括家庭和工作网络位置.公用网络位置设置.netsh firewall set opmode mode=disable pro ...

  6. 图的遍历——DFS和BFS模板(一般的图)

    关于图的遍历,通常有深度优先搜索(DFS)和广度优先搜索(BFS),本文结合一般的图结构(邻接矩阵和邻接表),给出两种遍历算法的模板 1.深度优先搜索(DFS) #include<iostrea ...

  7. note 2 运算符和表达式

    运算符 +.-.*./ %求余Modulus **指数Exponent 表达式 #C = 5/9 (F - 32) print 5.0/9*(75-32) 自动类型转换 类型相同,结果类型不变 1/2 ...

  8. Oracle中存储图片的类型为BLOB类型,Java中如何将其读取并转为字符串?

    一,读取图片转为String类型: 需要使用Sun公司提供的Base64工具 String str = ((Map) list1.get(0)).get("EINVOICEFILE" ...

  9. Pascal语言(存档)

    数据类型 标准函数 运算符和表达式 输入语句 输出语句 if语句 case语句 for语句 while语句 repeat语句 函数与过程 形参与实参 全局变量与局部变量 数组 字符串 枚举 子界 集合 ...

  10. MyElipse10添加Git

    1.下载对应MyEclipse的egit,教程用的是22.2的 http://wiki.eclipse.org/EGit/FAQ#Where_can_I_find_older_releases_of_ ...