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 ...
随机推荐
- 【BZOJ-2818】Gcd 线性筛
2818: Gcd Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 3347 Solved: 1479[Submit][Status][Discuss ...
- CSS设置技巧
一.单位和值 1.1 颜色值 在网页中的颜色设置是非常重要,有字体颜色(color).背景颜色(background-color).边框颜色(border)等,设置颜色的方法也有很多种: 1.英文命令 ...
- groovy-位运算
从Groovy 1.0 beta 10开始,Groovy支持位运算:<<. >>, >>>, |, &, ^, and ~. 下表列出了位运算的操作符 ...
- POJ2309BST(树状数组)
BST Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9182 Accepted: 5613 Description C ...
- Java Observer 观察者
http://www.cnblogs.com/jaward/p/3277619.html 1.API 被观察者 java.util.Observable; public class Observabl ...
- Python2.x与Python3.x的区别
这个星期开始学习Python了,因为看的书都是基于Python2.x,而且我安装的是Python3.1,所以书上写的地方好多都不适用于Python3.1,特意在Google上search了一下3.x和 ...
- 实现ajax
啊打发 <script type="text/javascript" src="js/jquery-1.8.3.min.js"></scrip ...
- 新浪微博客户端(11)-自定义checkBox
在最后一个欢迎界面上添加一个CheckBox. // 2.添加4个UIImageView ; i < NEW_FEATURE_NUMS; i++) { UIImageView *imageVie ...
- js获取单选框里面的值
rt,如果想获取单选框里面的值,该如何获取呢. <script> window.onload = function(){ //通过名字获取 getElementsByName //var ...
- 让chrome浏览器变成在线编辑器
在大部分人眼里,技术宅给人的印象是沉默寡言,总摸不透他心里想些什么,彼此都保持距离.作为半个程序员,我觉得真正的技术宅大部分时间都在找乐子,鼓捣各种想法,和大部分人的极客心理是一样的,程序员也还爱讲笑 ...