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 ...
随机推荐
- 什么是POJO?
本文转载自百度文库,详细出处请参考: http://wenku.baidu.com/view/4a9ad533ee06eff9aef80765.html 我认为写的很准确,很抱歉没有找到作者的名字! ...
- RBAC(基于角色的访问控制权限)表结构
Rbac 支持两种类,PhpManager(基于文件的) 和 DbManager(基于数据库的) 权限:就是指用户是否可以执行哪些操作 角色:就是上面说的一组操作的集合,角色还可以继承 在Yii2.0 ...
- 洛谷P1263 || 巴蜀2311 宫廷守卫
题目描述 从前有一个王国,这个王国的城堡是一个矩形,被分为M×N个方格.一些方格是墙,而另一些是空地.这个王国的国王在城堡里设了一些陷阱,每个陷阱占据一块空地. 一天,国王决定在城堡里布置守卫,他希望 ...
- Openjudge 8782 乘积最大
伤心,感冒了根本没精力肝题,只能做点小的 描述 今年是国际数学联盟确定的“2000——世界数学年”,又恰逢我国著名数学家华罗庚先生诞辰90周年.在华罗庚先生的家乡江苏金坛,组织了一场别开生面的数学智力 ...
- JSP 九个隐含JSP对象
输入输出对象:request.response.out. 作用域通信对象:session.application.pageContext servlet对象:page.config 错误对象:exce ...
- 多线程练习(java)
public class TestThread { public static void main(String[] args) { RandomNumber r=new RandomNumber() ...
- android选择时间攻略
安卓开发过程中难免会碰到需要选择日期时间的情况,由于大部分android初级教程都没教怎么选择时间,初学者碰到这种难免会有些不知所措,难道要让用户自己输入日期时间?先不说用户体验不好,处理用户输入各式 ...
- linux (centos) 单机50w+链接 内核参数配置
1 突破系统最大fd 查看当前文件描述符的限制数目的命令: ulimit -n .修改文件描述符的限制数目 2.1 临时改变当前会话: ulimit -n 2.2 永久变更需要下面两个步骤: ./ ...
- apache-tomcat-5.5.35.搭建实战
tar xf apache-tomcat-5.5.35.tar -C /usr/local/ 需要安装JDK支持,可借鉴:http://www.cnblogs.com/zhoulf/archive/2 ...
- WPF 操作键盘
#region 打开键盘的键 const uint KEYEVENTF_EXTENDEDKEY = 0x1; const uint KEYEVENTF_KEYUP = 0x2; [DllImport( ...