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 ...
随机推荐
- Hadoop插件安装
1.首先下载Hadoop对应版本的插件,以Hadoop 1.0版本对应的插件Hadoop-eclipse-plugin1.0.3.jar为例 2.将下载的插件放置到Ecplise安装目录的plugin ...
- spring 入门篇
spring 入门篇 相对于Hibernate(冬眠),Spring(春天),具有更多的诗意与希望的感觉,是为了解决传统J2EE开发效率过低.开发商之间不统一.没有真正实现“写一次到处 ...
- 怎样使用Markdown
转自:http://wowubuntu.com/markdown/basic.html 段落.标题.区块代码 一个段落是由一个以上的连接的行句组成,而一个以上的空行则会划分出不同的段落(空行的定义是显 ...
- JQ插件开发方法
由于项目原因,不得不写个JQ侧滑插件来满足需求.. 先引用两篇博文,待测试了 再写怎么做.. http://blog.csdn.net/business122/article/details/8278 ...
- C#学习日志 day 2 plus ------ hyper-V 开启方法
hyper-V的开启需要两个步骤. 第一是在bios中开启 virtualization technology--虚拟化技术 在process setting中改为enabled. 进入bios界面的 ...
- 【原创】Libjpeg 库使用心得(一) JPEG图像DCT系数的获取和访问
[原创]继续我的项目研究,现在采用Libjpeg库函数来进行处理,看了库函数之后发现C语言被这些人用的太牛了,五体投地啊...废话不多说,下面就进入正题. Libjpeg库在网上下载还是挺方便的,这里 ...
- Vue.jsbrowserify项目模板
Vue.js——60分钟browserify项目模板快速入门 概述 在之前的一系列vue.js文章,我们都是用传统模式引用vue.js以及其他的js文件的,这在开发时会产生一些问题. 首先,这限定 ...
- Eclips入门教程
1. 插件推荐 Eclipse默认情况下是一个纯净版的,所以功能简单,而开源IDE最为强大的莫过于各种插件,通过使用插件可以帮助我们减少大量编写代码的工作量,也帮助我们降低了编写代码的难度,所以懂得安 ...
- 7_Table Views
7 // // ViewController.swift // Table Views // // Created by ZC on 16/1/9. // Copyright © 2016年 ZC. ...
- PE头的应用---插入代码到EXE或DLL文件中
三.代码实现(DELPHI版本),采用第三种方式实现代码插入. 1. 定义两个类,一个用来实现在内存中建立输入表:一个用来实现对PE头的代码插入. DelphiCode: const MAX_SECT ...