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. R语言中的字符串处理函数

    内容概览   尽管R是一门以数值向量和矩阵为核心的统计语言,但字符串有时候也会在数据分析中占到相当大的份量.   R语言是一个擅长处理数据的语言,但是也不可避免的需要处理一些字符串(文本数据).如何高 ...

  2. Dynamics CRM Plug-in

    Plug-in 就是我们俗称的dll file 或者是assembly file. 里面有自定义的代码可以运行在服务器端 Plug-in Pipeline: 只有3个阶段可以做改动: Pre-Vali ...

  3. 引入public文件目錄下js/css文件

    <link href="{{ URL::asset('css/ySelect.css') }}" rel="stylesheet" type=" ...

  4. SVN简单使用

    如果是window操作系统,默认安装.右键菜单就会有显示SVN 如果已经配置好SVN,直接确定既可以检出. 如果没有配置,那么会显示下面的验证: 输入用户名和密码即可

  5. Spring Cloud(Dalston.SR5)--Zuul 网关-过滤器

    Spring Cloud 为 HTTP 请求的各个阶段提供了多个过滤器,这些过滤器的执行顺序由各自提供的一个 int 值决定,提供的值越小则优先级越高,默认的过滤器及优先级如下: 自定义过滤器 在默认 ...

  6. Matlab中调用VS编译的exe文件并传递变量 的方法

    经历::在网上找了很多方法,都没有实现在matlab中调用vs的exe文件并且能够传递变量参数,一些小细节花费了自己很多时间,比喻忽略了一些空格!  网上很多的方法都是纯粹复制别人的方法,自己都没有去 ...

  7. nginx 代理flask应用的uwsgi配置

    socket代理配置: 关于uwsgi的用法,请自行百度,这里只针对socket文件和端口的不同,进行单一的记录. 这种方式启动的flask应用,由于是通过socket与nginx通信的,所以必须制定 ...

  8. Dapper/SqlMapper映射对应问题

    Dapper, a light weight object mapper for ADO.NET 轻量级orm辅助类,不用配置,直接引入一个cs文件或者引入其dll就能使用. 使用连接对象的query ...

  9. activity--生命周期总结

    22.Android禁止屏幕旋转 & 旋转屏幕时保持Act内容? 21.旋转屏幕Act执行的生命周期方法? 12.ActA与 ActB互相跳转生命周期情况? 11.Act的生命周期? ==== ...

  10. WPF 重写ListBox(透明效果)

    <UserControl d:DesignHeight="460" d:DesignWidth="300" x:Name="UCcontrol& ...