Java for 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).
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的更多相关文章
- Java for LeetCode 063 Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- 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). ...
- [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 ...
- 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 ...
- 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). ...
- 【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). ...
- 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 ...
- [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 ...
- [Leetcode Week12]Unique Paths II
Unique Paths II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths-ii/description/ Descrip ...
随机推荐
- [NOIP2008] 提高组 洛谷P1155 双栈排序
题目描述 Tom最近在研究一个有趣的排序问题.如图所示,通过2个栈S1和S2,Tom希望借助以下4种操作实现将输入序列升序排序. 操作a 如果输入序列不为空,将第一个元素压入栈S1 操作b 如果栈S1 ...
- LABJS使用教程
知道LABJS这个概念其实早于sea.js,但因为sea.js是中文,并且第一眼就喜欢上sea.js的CommonJS所以并没有深入了解过LABJS. 在使用sea.js的时候不可避免的碰到js文件依 ...
- Android学习笔记02-Mac下编译java代码
在Mac OS上配置JDK 1.7. 一 下载 Mac版本的JDK1.7 从以下下载地址,下载Mac版本的JDk1.7 安装文件 jdk-7u79-macosx-x64.dmg. http://www ...
- CentOS 6.4安装Apache+MySQL+PHP的图文教程
LAMP 实际上就是 Linux.Apache.MySQL.PHP 四个名称的缩写,当然最后一个 “P” 还有其他说法是 Perl 或者 Python.不用多说了,本文讲解的就是 Linux.Apac ...
- VirtualBox安装debian的详细方法步骤
下面是用VirtualBox安装Debian6的方法和步骤 l 新建一个文件夹,用于存放虚拟硬盘,如Debian l 打开VirtualBox,点击新建 l 输入虚拟机名称,Debian_6 l 给虚 ...
- linux 搭建SVN服务器,为多个项目分别建立版本库并单独配置权限
1.安装svn服务 # yum install subversion 2.新建一个目录用于存储SVN所有文件 # mkdir /home/svn 3.在上面创建的文件夹中为项目 p ...
- html5+ 获取当前设备的加速度信息
getCurrentAcceleration 获取当前设备的加速度信息 void plus.accelerometer.getCurrentAcceleration( successCB, error ...
- xss实例-什么都没过滤的情况
1. XSS的存在,一定是伴随着输入,与输出2个概念的. 2. 要想过滤掉XSS,你可以在输入层面过滤,也可以在输出层面过滤. 3. 如果输入和输出都没过滤. 那么漏洞将是显而易见的. 4. 作为第一 ...
- mouseover和mouseout闪烁问题
在父级元素上注册了mouseover和mouseout事件后,当鼠标移动到子元素上时,会触发父级的mouseout和mouseover事件,反复触发,形成闪烁. 原因: 一种是由于冒泡,子级的mous ...
- Windows 下的.NET+ Memcached安装
转载请标明出处: http://www.yaosansi.com/ 原文:http://www.yaosansi.com/post/1396.html Memcached官方:http://danga ...