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?
Above is a 7 x 3 grid. How many possible unique paths are there?
Note: m and n will be at most 100.
Example 1:
1 | Input: m = 3, n = 2 |
Example 2:
1 | Input: m = 7, n = 3 |
思路:仔细读题分析后发现其实是一个排列组合问题,我们将机器人向右走记做0,向下走记做1,则在一个mxn的大格子中,我们走到右下角的终点,不管以何种路径走,最后必然有m-1个0和n-1个1。所以问题就转化成了从n+m-2个位置中选择m-1个0或者n-1个1有多少种选法的问题了。
因为int型会溢出,所以用了BigInteger。
代码如下:
1 | import java.math.BigInteger; |