62.Unique Paths---dp
题目大意:给一个m*n的方格,从左上角走到右下角,中间无任何障碍,问有多少种走法。
法一:DFS,超时,简单模板深搜,无任何剪枝,结果一半的数据超时。代码如下:
public int uniquePaths(int m, int n) {
int f[][] = {{0, 1}, {1, 0}};
boolean vis[][] = new boolean[m][n];
return dfs(m, n, 0, 0, 0, f, vis);
}
public static int dfs(int m, int n, int x, int y, int cnt, int f[][], boolean vis[][]) {
if(x == m - 1 && y == n - 1) {
System.out.println("answer:" + cnt);
cnt++;
return cnt;
}
for(int i = 0; i < 2; i++) {
int cnt_x = x + f[i][0];
int cnt_y = y + f[i][1];
if(cnt_x < m && cnt_y < n && vis[cnt_x][cnt_y] == false) {
vis[cnt_x][cnt_y] = true;
cnt = dfs(m, n, cnt_x, cnt_y, cnt, f, vis);
vis[cnt_x][cnt_y] = false;
}
}
return cnt;
}
法二(借鉴):很简单的dp,dp[i][j]表示当终点坐标是[i,j]时,所有可能的路径总数代码如下(耗时1ms):
public int uniquePaths(int m, int n) {
int dp[][] = new int[m][n];
//初始化
//对于第一列和第一行,走的路径数应该初始化为1
for(int i = 0; i < m; i++) {
dp[i][0] = 1;
}
for(int i =0 ; i < n; i++) {
dp[0][i] = 1;
}
//计算dp
//对于dp[i][j],每一个坐标[i,j],都可以由其左侧和上侧走一步而来。类似于杨辉三角
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,还不是很懂,是怎么来的,暂且记一下。代码如下(耗时0ms):
public int uniquePaths(int m, int n) {
int dp[] = new int[n];
//初始化,dp[i]表示某一行第i列的路径总数
for(int i = 0; i < n; i++) {
dp[i] = 1;
}
//外层循环是每一行,第1行计算了,第2行就是利用了第1行的值,所以这里也是左+上的和得到当前值,只是节约了空间
for(int i = 1; i < m; i++) {
//对于每一行,逐一判定每一列的路径值。
for(int j = 1; j < n; j++) {
dp[j] += dp[j - 1];
}
}
//两层循环后,得到的最终的值就是所有路径的结果值。
return dp[n - 1];
}
法四(借鉴):数学方法,由左上到右下,向下要走m-1步,向右要走n-1步,也就是要走C(m+n-2, m-1)或C(m+n-2, n-1)步。而C(m, n) = A(m, n)/n!,又A(m, n)=m!/(m-n)!。则C(m+n-2, m-1)=m*(m+1)*(m+2)...*(m+n-2)/1*2*3...*(n-1)。代码如下:
public int uniquePaths(int m, int n) {
double res = 1;
//计算组合数
for(int i = 1; i <= n - 1; i++) {
res = res * (m + i - 1) / i;
}
return (int)res;
}
62.Unique Paths---dp的更多相关文章
- leetcode 62. Unique Paths 、63. Unique Paths II
62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...
- 刷题62. Unique Paths
一.题目说明 题目62. Unique Paths,在一个m*n矩阵中,求从左上角Start到右下角Finish所有路径.其中每次只能向下.向右移动.难度是Medium! 二.我的解答 这个题目读读题 ...
- 62. Unique Paths(中等,我自己解出的第一道 DP 题^^)
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- 62. Unique Paths (Graph; DP)
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 ...
- 62. Unique Paths && 63 Unique Paths II
https://leetcode.com/problems/unique-paths/ 这道题,不利用动态规划基本上规模变大会运行超时,下面自己写得这段代码,直接暴力破解,只能应付小规模的情形,当23 ...
- 62. Unique Paths
题目: A robot is located at the top-left corner of a m x ngrid (marked 'Start' in the diagram below). ...
- LeetCode OJ 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】#62. Unique Paths
一天一道LeetCode系列 (一)题目 A robot is located at the top-left corner of a m x n grid (marked 'Start' in th ...
- [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 ...
随机推荐
- Using the command line to manage files on HDFS--转载
原文地址:http://zh.hortonworks.com/hadoop-tutorial/using-commandline-manage-files-hdfs/ In this tutorial ...
- MSSQL数据库分页存储过程
create procedure [dbo].[p_splitpage] ), , , output, output as set nocount on declare @p1 int ,,@rowc ...
- java利用poi读取excel异常问题
最近一个web工程需要完成一个小功能,利用文件上传然后读取文件内容写入到数据库,这里是操作的excel文件,excel文件分两种后缀,03版本的xls和之后的xlsx,现在大家一般都拿非常好用的插件直 ...
- 【BZOJ2876】【NOI2012】骑行川藏(数学,二分答案)
[BZOJ2876][NOI2012]骑行川藏(数学,二分答案) 题面 BZOJ 题解 我们有一个很有趣的思路. 首先我们给每条边随意的赋一个初值. 当然了,这个初值不会比这条边的风速小. 那么,我们 ...
- NOIP2015运输计划题解报告
这题在洛谷上可以找到提交 P2680运输计划 题目背景 公元 2044 年,人类进入了宇宙纪元. 题目描述 L 国有 n 个星球,还有 n-1 条双向航道,每条航道建立在两个星球之间,这 n-1 条航 ...
- 解题:POI 2007 Weights
题面 这是个$O(nlog^2$ $n)$的解法,因为蒟蒻博主没有看懂$O(nlog$ $n)$的更优秀的解法 显然从小到大装砝码是最优的方法,又显然从大到小装容器不会使得答案变劣,还显然砝码数具有单 ...
- Winform中的Treeview动态绑定数据库
http://bbs.csdn.net/topics/370139193 SQL code ? 1 2 3 4 5 6 CREATE TABLE [dbo].[Company] ( [Id ...
- varchar字段
varchar 最长26000多,实际使用最好不要超过255,会占内存 可以考虑text
- eclipse+myeclipse 使用技巧备忘
myeclipse 导入多模块maven项目 https://blog.csdn.net/jack85986370/article/details/51371853 maven项目在eclipse的l ...
- centos7 nginx开启启动
centos 7以上是用Systemd进行系统初始化的,Systemd 是 Linux 系统中最新的初始化系统(init),它主要的设计目标是克服 sysvinit 固有的缺点,提高系统的启动速度.关 ...