首页 > 编程语言 > 详细

Java for LeetCode 062 Unique Paths

时间:2015-05-15 22:40:07      阅读:337      评论:0      收藏:0      [点我收藏+]

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?

解题思路一:

BigInteger,JAVA实现如下:

import java.math.BigInteger;

public class Solution {
	public int uniquePaths(int m, int n) {
		if (n > m) {
			int temp = n;
			n = m;
			m = temp;
		}
		return Amn(m + n - 2, n - 1).divide(factorial(n - 1)).intValue();
	}

	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));
	}
}

 

Java for LeetCode 062 Unique Paths

原文:http://www.cnblogs.com/tonyluis/p/4506913.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!