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 ...
随机推荐
- 写在SDOI2016Round1前的To Do List
理性的整理了一下自己的不足. 计算几何啥都不会,字符串类DP毫无练习,数据结构写的不熟,数论推不出式子,网络流建模常建残: 需要达成的任务: 一.网络流: 熟练网络流的板子(之前一直仰慕zkw费用流, ...
- 洛谷P1755 斐波那契的拆分
题目背景 无 题目描述 已知任意一个正整数都可以拆分为若干个斐波纳契数,现在,让你求出n的拆分方法 输入输出格式 输入格式: 一个数t,表示有t组数据 接下来t行,每行一个数n(如题) 输出格式: t ...
- eclipse中新建python项目报错:Project interpreter not specified
eclipse-windows-preferences-python
- Spring AOP 系列总括
Spring有两大核心,IOC和AOP.IOC在Java Web项目中无时无刻不在使用,然而AOP用的比较少,尤其是对一些初级程序员,在架构师搭好的框架上开发应用代码,AOP几乎是透明的.然而,项目中 ...
- ExtJS入门教程04,这是一个超级好用的grid
今天进行extjs入门教程的第四篇:grid. 来一份grid尝尝 小伙伴们都知道extjs的grid功能强大,更清楚功能强大的东西用起来必然会复杂.今天我们就从最简单的grid开始讲解. 先来一个最 ...
- vagrant 错误记录
使用Vagrant配置本地开发环境 从二零一四年开始使用vagrant+VirtualBox搭建linux开发环境,配置简单灵活,后台运行占用内存少,比vmware好用很多,果断弃用vmware转投v ...
- 用 xampp 在 windows/Linux 下搭建代理服务器
背景:学校上网开始收费,但实验室免费,由于宿舍和实验室都有ipv6,所以在实验室搭建代理服务器,让宿舍通过之上网. Windows下 两步: 1.编辑httpd.comf,去掉关于proxy的注释 L ...
- linux (centos) 单机50w+链接 内核参数配置
1 突破系统最大fd 查看当前文件描述符的限制数目的命令: ulimit -n .修改文件描述符的限制数目 2.1 临时改变当前会话: ulimit -n 2.2 永久变更需要下面两个步骤: ./ ...
- Nginx Http框架的理解
Nginx Http框架的理解 HTTP框架是Nginx基础框架的一部分,Nginx的其它底层框架如master-worker进程模型.event模块.mail 模块等. HTTP框架代码主要有2个模 ...
- SQL injection
SQL injection is a code injection technique, used to attack data-driven applications, in which malic ...