Skip to content

Commit c13a11f

Browse files
authored
Create unique_paths.md
1 parent 58908cd commit c13a11f

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

unique_paths.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Unique Paths
2+
## Problem
3+
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).
4+
5+
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).
6+
7+
How many possible unique paths are there?
8+
9+
![图片](https://leetcode.com/static/images/problemset/robot_maze.png)
10+
11+
Above is a 3 x 7 grid. How many possible unique paths are there?
12+
13+
Note: m and n will be at most 100.
14+
## 问题分析
15+
向下走和向右走的步数是一定的,一共走m+n-2步,其中m-1步往一个方向走,n-1步往另一个方向走,该题就转化为了排列组合的问题,从m+n-2步中选出m-1步来或从m+n-2选出n-1步来,结果都是一样的
16+
## 代码实现
17+
```ruby
18+
int uniquePaths(int m, int n) {
19+
long x m+n-2; //一定要用long,阶乘数太大了
20+
long y=(m<n?m:n)-1;
21+
long up=1,down=1;
22+
if(m==1||n==1)
23+
return 1;
24+
for(int i=0;i<y;i++){
25+
up *= x--;
26+
}
27+
for(int i=y;i>0 ;i--){
28+
down *= i;
29+
}
30+
return (int)(up/down);
31+
}
32+
```

0 commit comments

Comments
 (0)