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?

解题思路一:

很明显,答案是C(m+n-2,n-1),是一个排列组合问题,直接使用int进行操作的话,会发生数据溢出,不过好在我们有BigInteger,JAVA实现如下:

import java.math.BigInteger;

public class Solution {
public int uniquePaths(int m, int n) {
return Cmn(m,n).intValue();
}
static BigInteger Cmn(int m, int n){
return Amn(m + n - 2, n - 1).divide(factorial(n - 1));
}
static BigInteger Amn(int m, int n) {
if (n == 0)
return BigInteger.valueOf(1);
if (n == 1)
return BigInteger.valueOf(m);
else
return Amn(m - 1, n - 1).multiply(BigInteger.valueOf(m));
} static BigInteger factorial(int n) {
if (n == 1 || n == 0)
return BigInteger.valueOf(1);
else
return factorial(n - 1).multiply(BigInteger.valueOf(n));
}
}

解题思路二:

很明显uniquePaths(int m, int n)=uniquePaths(int m-1, int n)+uniquePaths(int m, int n-1),结果提交发现Time Limit Exceeded,看来不能直接用递归计算。不过基于上述结论,我们可以采用dp的方法,开一个数组v[j]表示经过i步右移和j步下移后达到(i,j)的路径数目,前进方法为v[j] += v[j - 1],表示v[j]由从左边过来的v[j-1]和从上面下来的前一个v[j]组成,JAVA实现如下:

	static public int uniquePaths(int m, int n) {
int[] v = new int[n];
for (int i = 0; i < v.length; i++)
v[i] = 1;
for (int i = 1; i < m; ++i)
for (int j = 1; j < n; j++)
v[j] += v[j - 1];
return v[n - 1];
}

Java for LeetCode 062 Unique Paths的更多相关文章

  1. Java for LeetCode 063 Unique Paths II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  2. 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). ...

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

  4. Java for LeetCode 095 Unique Binary Search Trees II

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  5. LeetCode 62. Unique Paths不同路径 (C++/Java)

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

  6. 【LeetCode】062. Unique Paths

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

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

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

  9. [Leetcode Week12]Unique Paths II

    Unique Paths II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths-ii/description/ Descrip ...

随机推荐

  1. 【前端学习】git命令行界面

    学习目标:掌握git命令行界面的操作.掌握最基本的clone add commit push pull操作. 先下载客户端:http://github-windows.s3.amazonaws.com ...

  2. Spring POST

    Spring POST+表单对象如果数据格式不正确,则页面直接报400.页面没有反应,烦啊

  3. 一个糟糕的Erlang练习题

    好吧,用的语法很糟糕...但是至少是做了练习. 题目 %The sequence of triangle numbers is generated by adding the natural numb ...

  4. C++ 中的名称冲突之 "y1"

    已经是第二次遇到这个问题了: #include <bits/stdc++.h> using namespace std; ); ][N][][N]; int x1, x2, y1, y2; ...

  5. HD2157How many wasy??(十大矩阵问题之八 + 邻接矩阵的应用)

    How many ways?? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  6. Android 实现卫星菜单(精简版)

    MainActivity.java public class MainActivity extends AppCompatActivity { private ArcDemo mArc; privat ...

  7. 用Nikto探测一个网站所用到的技术

    Nikto是一款开源的(GPL)网页服务器扫描器,它可以对网页服务器进行全面的多种扫描,包含超过3300种有潜在危险的文件/CGIs:超过 625种服务器版本:超过230种特定服务器问题,包括多种有潜 ...

  8. Centos 5.x/6.x 配置163网易yum源

    Centos系统默认都是系统自带的yum源,国内用户用yum源安装比较慢,为了提高效率,一般我们会配置国内的yum源.国内比较好的yum源有网易yum源.搜狐yum源等. 我感觉网易的yum源比较好用 ...

  9. MySQL主从分离读写复制

    参考教程:http://www.yii-china.com/post/detail/283.html MySQL是开源的关系型数据库系统.复制(Replication)是从一台MySQL数据库服务器( ...

  10. js实现鼠标点击input框后里面的内容就消失代码

    <!--# <a href="http://www.mianfeimoban.com/texiao_mb/" target="_blank" cla ...