【动态规划】LeetCode 62. Unique Paths
LeetCode 62. Unique Paths
Solution1:我的未能AC的答案
递归超时了!!!
class Solution {
public:int uniquePaths(int m, int n) {int res = 0;my_unique(n, m, res, 0, 0);return res;}void my_unique(int row, int col, int& res, int i, int j) {if (i >= row || j >= col)return;else if (i == row - 1 && j == col - 1) {res = res + 1;return;}else {my_unique(row, col, res, i + 1, j);my_unique(row, col, res, i, j + 1);}}
};
Solution2:
参考网址:http://www.cnblogs.com/grandyang/p/4353555.html
利用排列组合的公式:
A_n^m=\frac{n!}{(n-m)!}
C_n^m=\frac{A_n^m}{m!}=\frac{n!}{m!(n-m)!}=\frac{n(n-1)...(n-m+1)}{m!}
机器人一共走了m+n-2步,任意挑m-1步向右即可!
CmnCnmC_n^m和 Cn−mnCnn−mC_n^{n-m}是一样的,但为了防止溢出在循环时利用较小值进行计算!!!
class Solution {
public:int uniquePaths(int m, int n) {double a = 1, b = 1, N = m + n - 2, M = m > n? n - 1: m - 1;for (int i = 1; i <= M; i++) {a *= N + 1 - i;b *= i;}return a / b;}
};
Solution3:
动态规划,参考网址:http://www.cnblogs.com/grandyang/p/4353555.html
//DP维护二维数组
class Solution {
public:int uniquePaths(int m, int n) {vector<vector<int> > dp(m, vector<int> (n, 1));for (int i = 1; i < m; ++i) for (int j = 1; j < n; ++j) dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; return dp[m - 1][n - 1];}
};//DP维护一维数组
class Solution {
public:int uniquePaths(int m, int n) {vector<int> dp(n, 1);for (int i = 1; i < m; ++i) {for (int j = 1; j < n; ++j) {dp[j] += dp[j - 1]; }}return dp[n - 1];}
};
【动态规划】LeetCode 62. Unique Paths相关推荐
- [Leetcode]62. Unique Paths
62. Unique Paths 本题难度: Easy Topic: Dynamic Programming Description A robot is located at the top-lef ...
- 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). ...
- [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). ...
- [leetcode] 62 Unique Paths (Medium)
原题链接 字母题 : unique paths Ⅱ 思路: dp[i][j]保存走到第i,j格共有几种走法. 因为只能走→或者↓,所以边界条件dp[0][j]+=dp[0][j-1] 同时容易得出递推 ...
- [swift] 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 ...
- [dp] LeetCode 62. Unique Paths
输入:两个int m和n 输出:一个int,表示不同路径的个数. 规则:有一个m行n列的矩阵,一个机器人从左上角走到右下角,每次向下或者向右走一格. 分析:目的是要找到从(0,0)到(m-1,n-1) ...
- 【动态规划】LeetCode 63. Unique Paths II
LeetCode 63. Unique Paths II Solution1:我的答案 在哪里做过这题? class Solution { public:int uniquePathsWithObst ...
- [Lintcode]115. Unique Paths II/[Leetcode]63. Unique Paths II
115. Unique Paths II/63. Unique Paths II 本题难度: Easy/Medium Topic: Dynamic Programming Description Fo ...
- leetcode 【 Unique Paths 】python 实现
题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...
最新文章
- python input 数字_Python:raw_input读取数字的问题
- 请求令牌 接口_时序图说明JWT用户认证及接口鉴权的细节
- 「每周CV论文推荐」 初学深度学习活体与伪造人脸检测必读的文章
- ASP.NET连接带密码Access的方法
- python 字符串补齐
- 阿里1682亿背后的协同研发云——云效正式商业化
- Highcharts JS去除Highcharts.com链接的方法
- DCMTK:测试CT Table Dynamics FG类
- 20170429,上市公司2016年报全出炉(附最新排行榜)
- 10以内的分解与组成怎么教_狗狗酷炫的飞盘游戏怎么玩?分解步骤教你快速学会...
- JAR——pinyin4j-2.5.0
- 基于SpringMVC进行REST服务开发
- 实现基于最近邻内插和双线性内插的图像缩放C++实现
- shell条件检查原理:command echo ‘success‘ || echo ‘error‘
- Nmap 源代码学习四 软件简单使用
- itunes安装后不能用,双击后等很长时间,提示:ITUNES 驱动程序缺少用于导入和刻录的CD与DVD注册的设置...
- InnoDB怎么解决幻读的?
- java 下拉复选框_JAVA个人小程序GUI篇-收银(标签、按钮、复选框、下拉标、文本域、表格······)...
- mri计算机系统,MRI的一些基本介绍
- 3D汽车作品大赏!汇集世界各地CG大佬们的“汽车梦”
热门文章
- 深度学习之----双线性插值,转置卷积,反卷积的区别与联系
- 详细解读ORBSLAM中的描述子提取过程
- vscode php插件_「PHP从入门到颈椎病康复」基础篇——HelloWorld
- pygame重新开始_Pygame(十八)音乐
- c ++ strstr_在C / C ++中使用strstr()的指南
- Python字符串isdecimal()
- java中文件处理之图片_Java中的文件处理
- python主要功能_Python主要功能
- 面试 restful_RESTful Web服务面试问题
- ctf web必备工具_设计人员和开发人员的必备Web工具和服务