Leetcode 动态规划 Unique Paths
本文为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的更多相关文章
- 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). ...
- [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 ...
- [Leetcode Week12]Unique Paths II
Unique Paths II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths-ii/description/ Descrip ...
- [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 ...
- [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 ...
- 【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 ...
- 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 ...
- 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 ...
- [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 ...
随机推荐
- C++中的unordered_map
1.简介 随着C++0x标准的确立,C++的标准库中也终于有了hash table这个东西.很久以来,STL中都只提供<map>作为存放对应关系的容器,内部通常用红黑树实现,据说原因是二叉 ...
- JAVA之GUI编程ACTION事件
package GUI; import java.awt.Button; import java.awt.FlowLayout; import java.awt.Frame; import java. ...
- js命名空间的使用
js命名空间的使用: test.html 代码如下: <!DOCTYPE HTML><html lang="en-US"><head> & ...
- windows下搭建python+selenium环境
1.安装python https://www.python.org/ 2.安装setuptools(python的基础包工具) 下载地址:https://pypi.python.org/pypi/se ...
- Howie带你云上飘-新浪云
介绍一下怎么在sae上做个网站 前言 曾经,sae是收费的,计时收费,还挺贵的呢.所以就试玩了一下,没敢继续鼓捣.后来,云计算越来越火了,新浪也不差钱嘛,于是直接给新注册的开发者送好多豆子,于是,免费 ...
- Delphi XE的RTTI增强,动态Hook某些内部事件
Delphi2010之后的RTTI做了很大休整,现在用起来很爽了哦.甚至可以获取某些类的内部私有单元,然后为其赋值!讲这个RTTI增强的,可以参考网上的多个博客内容,我列举一下: Delphi2010 ...
- C# WinForm判断Win7下是否是管理员身份运行
原文:C# WinForm判断Win7下是否是管理员身份运行 如果程序不是以管理员身份运行,操作本地文件会提示:System.UnauthorizedAccessException异常 Vista 和 ...
- linux下安装python3.3.4
下载安装包 # wget http://www.python.org/ftp/python/3.3.4/Python-3.3.4.tgz 解压 # tar -xzvf Python-3.3.4.tgz ...
- golang printf
1: 打印包括字段在内的实例的完整信息 同 %+V fmt.Printf("Hello world! %v","hufeng") 输出:Hello world ...
- css3属性选择器总结
前两节介绍了css3属性选择器与css2属性选择器中: 包含字符串和以字符串选择器开头的选择器的比较. 全部属性选择器: 包含字符串讲解对比实例讲解链接: http://www.cnblogs.com ...