题目链接

题目大意:给一个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的更多相关文章

  1. leetcode 62. Unique Paths 、63. Unique Paths II

    62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...

  2. 刷题62. Unique Paths

    一.题目说明 题目62. Unique Paths,在一个m*n矩阵中,求从左上角Start到右下角Finish所有路径.其中每次只能向下.向右移动.难度是Medium! 二.我的解答 这个题目读读题 ...

  3. 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 ...

  4. 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 ...

  5. [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 ...

  6. 62. Unique Paths && 63 Unique Paths II

    https://leetcode.com/problems/unique-paths/ 这道题,不利用动态规划基本上规模变大会运行超时,下面自己写得这段代码,直接暴力破解,只能应付小规模的情形,当23 ...

  7. 62. Unique Paths

    题目: A robot is located at the top-left corner of a m x ngrid (marked 'Start' in the diagram below). ...

  8. 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 ...

  9. 【一天一道LeetCode】#62. Unique Paths

    一天一道LeetCode系列 (一)题目 A robot is located at the top-left corner of a m x n grid (marked 'Start' in th ...

  10. [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 ...

随机推荐

  1. MachineLearning ---- lesson 2 Linear Regression with One Variable

    Linear Regression with One Variable model Representation 以上篇博文中的房价预测为例,从图中依次来看,m表示训练集的大小,此处即房价样本数量:x ...

  2. oracle获取clob调优

    引用Oracle.DataAccess.dll,,在oracle 安装目录下的D:\oracle\product\10.2.0\db_1\ODP.NET\bin\1.x\Oracle.DataAcce ...

  3. 【agc006C】Rabbit Exercise

    Portal --> agc006C Solution 啊感觉是好有意思的一道题qwq官方题解里面的说辞也是够皮的哈哈哈..(大概就是说如果你没有意识到那个trick的话这题这辈子都做不出来qw ...

  4. 【loj6179】Pyh的求和

    Portal -->loj6179 Solution ​  这题其实有一个式子一喵一样的版本在bzoj,但是那题是\(m\)特别大然后只有一组数据  这题多组数据== ​    首先根据\(\v ...

  5. SpringMVC 重定向

    在返回视图名字的字符串前面加forword:或redirect:前缀是就会对他们做特殊处理,它们分别是转发和重定向 我们测试一个重定向操作把 Java代码 @RequestMapping(" ...

  6. poj 1273 裸 网络流 (dinic)

    Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...

  7. socket编程学习step2

    引言:主机之间如何相互交互呢?网络层的“ip地址”可以唯一标识网络中的主机,而传输层的“协议+端口“可以唯一标识主机中的应用进程.这样利用三元组(ip地址,协议,端口)就可以标识网络的进程了,网络中的 ...

  8. libevent学习笔记(参考libevent深度剖析)

    最近自学libevent事件驱动库,参考的资料为libevent2.2版本以及张亮提供的<Libevent源码深度剖析>, 参考资料: http://blog.csdn.net/spark ...

  9. win32/linux 线程 log

    原文 #include <stdio.h> #include <stdlib.h> #include <string.h> #ifdef WIN32 #includ ...

  10. CSS3方法总汇

    PS:CSS3的3D和我做研发时的3D不一样他们只能旋转180度  横为X竖为Z高为Y transfrom:2D3D转换 rotareX:绕着X轴旋转 rotareY(-180deg):绕着Y轴旋转- ...