Leetcode 动态规划 Unique Paths
来源:互联网 发布:js print setup 插件 编辑:程序博客网 时间:2024/06/06 19:23
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie
Unique Paths
Total Accepted: 17915 Total Submissions: 57061My SubmissionsA 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">//思路1int 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] Unique Paths-动态规划
- [LeetCode]Unique Paths (动态规划)
- Leetcode 动态规划 Unique Paths II
- LeetCode Unique Paths 动态规划与大数
- LeetCode-62-Unique Paths(动态规划)-Medium
- [leetcode]unique-paths 动态规划 C++
- [LeetCode]62. Unique Paths&&动态规划
- leetcode 62. Unique Paths DP动态规划
- leetcode 62. Unique Paths-唯一路径|动态规划
- leetcode 63. Unique Paths II-唯一路径|动态规划
- [LeetCode 62] Unique Paths(教科书般的动态规划)
- 算法学习之动态规划(leetcode 62. Unique Paths)
- leetcode 63. Unique Paths II DP动态规划
- Leetcode解题笔记 62. Unique Paths [Medium] 动态规划
- Leetcode解题笔记 63. Unique Paths II [Medium] 动态规划
- Unique Paths 动态规划(重重)
- 53.Unique Paths(动态规划)
- MVC4中AJAX Html页面打开调用后台方法实现动态加载数据库中的数据
- c++基础学习4-c++函数b
- ls命令
- Hibernate查询之HQL查询
- muduo_net库源码分析
- Leetcode 动态规划 Unique Paths
- Have trouble in your life
- IOS自定义UIAlertView样式,实现可替换背景和按钮
- IOS获取来电去电系统通知达到来电归属地效果(一)
- makefile自动化变量及其说明
- JavaSe基础XX17——常用对象API-集合框架_2
- 多线程与并发服务器设计(23 - 2 )
- Java方法中有循环的时候返回值问题
- Oracle中数组的创建、赋值、遍历