本文为senlie原创。转载请保留此地址:http://blog.csdn.net/zhengsenlie

Unique Paths

Total Accepted: 17915 Total
Submissions: 57061My Submissions

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

Note: m and n will be at most 100.

题意:给定一个 m * n 的网格,一个机器人要从左上角走到右下角,每次仅仅能向下或向右移动一个位置。

问有多少种走法

思路1:dfs暴力枚举

复杂度:超时了... O(2^n)



思路2:记忆化搜索

用一个数组paths[i][j]记录从 (0,0) 到 (m,n)的路径数



思路3:dp

设置状态为f[i][j],表示从(0,0)到达网格(i,j)的路径数,则状态转移方程为

f[i][j] = f[i - 1][j] + f[i][j - 1]

复杂度:时间O(n^2) 空间 O(n)

<pre name="code" class="cpp">//思路1
int uniquePaths(int m, int n){
if(m < 0 || n < 0) return 0;
if(m == 1 && n == 1) return 1;
return uniquePaths(m - 1, n) + uniquePaths(m, n - 1);
} //思路2
//paths[i][j]表示从(0,0)到(i,j)的路径数
int paths[101][101];
int dfs(int m, int n){
if(m < 0 || n < 0) return 0;
if(m == 1 && n == 1) return 1;
if(paths[m][n] >= 0) return paths[m][n];
return paths[m][n] = dfs(m - 1, n) + dfs(m, n - 1);
}
int uniquePaths(int m, int n){
memset(paths, -1, sizeof(paths));
return dfs(m, n);
} //思路2还有一种写法
//paths[i][j]表示从(i,j)到(m - 1,n - 1)的路径数
int paths[101][101];
int mm, nn;
int dfs(int x, int y){
if(x >= mm || y >= nn) return 0;
if(x == mm - 1 && y == nn - 1) return 1;
if(paths[x][y] >= 0) return paths[x][y];
return paths[x][y] = dfs(x + 1, y) + dfs(x, y + 1);
}
int uniquePaths(int m, int n){
mm = m, nn = n;
memset(paths, -1, sizeof(paths));
return dfs(0, 0);
} //思路3 paths[i][j] 表示(0, 0) 到(i,j)的路径数
int paths[101][101];
int uniquePaths(int m, int n){
memset(paths, 0, sizeof(paths));
for(int i = 0; i < m; ++i) paths[i][0] = 1;
for(int j = 0; j < n; ++j) paths[0][j] = 1;
for(int i = 1 ; i < m; ++i){
for(int j = 1; j < n; ++j){
paths[i][j] = paths[i - 1][j] + paths[i][j - 1];
}
}
return paths[m - 1][n - 1];
}
思路3 还有一种写法
用一个一维数组 paths[j] 表示 (0, 0) 至 (i, j)的路径数,在外循环变量为 i 时,还没更新前
paths[j] 相应上面二维数组写法的paths[i - 1, j],paths[j - 1]相应paths[i][j - 1]
int paths[101];
int uniquePaths(int m, int n){
memset(paths, 0, sizeof(paths));
paths[0] = 1;
for(int i = 0; i < m; ++i){
for(int j = 1; j < n; ++j){
paths[j] = paths[j] + paths[j - 1];
}
}
return paths[n - 1];
}


Leetcode 动态规划 Unique Paths的更多相关文章

  1. 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). ...

  2. [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 ...

  3. [Leetcode Week12]Unique Paths II

    Unique Paths II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths-ii/description/ Descrip ...

  4. [LeetCode] 63. Unique Paths II 不同的路径之二

    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】Unique Paths

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

  7. leetcode 之 Unique Paths

    Unique Paths A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagra ...

  8. 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 ...

  9. [LeetCode] 63. Unique Paths II_ Medium tag: Dynamic Programming

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

随机推荐

  1. Yii2.0中文开发向导——自定义日志文件写日志

    头部引入log类use yii\log\FileTarget; $time = microtime(true);$log = new FileTarget();$log->logFile = Y ...

  2. MySQL mysqlimport 从txt文件中导入数据到mysql数据库

    mysqlimport: 我说这个我们还是先从世界观方法论的高度来理解一下便有更加准确的把握.数据导入不外呼有两个部分 第一部分:目标对象--我们要把数据导给谁(mysqlimport 的目标对象自然 ...

  3. 转:angular的decorator方法

    AngularJS实例 – 装饰$log 在AngularJS中,我们可以使用Angular内置或者自定义的services,在应用的各个部分之间分享数据和方法.假设你已经定义了一个service,但 ...

  4. java多线程同步

    一篇好文:java多线程机制同步原则 概括起来说,Java 多线程同步机制主要包含如下几点:1:如果一个类包含一个或几个同步方法,那么由此类生成的每一个对象都配备一个队列用来容纳那些等待执行同步的线程 ...

  5. Openstack命令收集

    查看rabbitmq 队列 rabbitmqctl list_queues 查看keystone的用户 keystone user-list 查看keystone endpoint keystone ...

  6. tpopela/vips_java

    tpopela/vips_java Implementation of Vision Based Page Segmentation algorithm in Java

  7. cocos android分析

    来自:http://xiebaochun.github.io/ cocos2d-x Android环境搭建 cocos2d-x环境搭建比較简单,可是小问题还是不少,我尽量都涵盖的全面一些. 下载软件  ...

  8. Objective-C分类 (category)和扩展(Extension)

    1.分类(category) 使用Object-C中的分类,是一种编译时的手段,允许我们通过给一个类添加方法来扩充它(但是通过category不能添加新的实例变量),并且我们不需要访问类中的代码就可以 ...

  9. 编写可维护的JS 02

    2.注释 单行 //单行注释 多行 /* 多行注释 */ /** * 多行注释 * */ 使用注释 使用注释的原则是让代码更清晰 难于理解的代码 难于理解的代码都应添加注释 可能被误认为错误的代码 应 ...

  10. Linux新手笔记 源 安装chromium

    centos6.4 32 一.软件源目录/etc/yum.repos.d把新的软件源文件copy到这即可.二.安装chromiumwget http://people.centos.org/hughe ...