# [leetcode] 62. Unique Paths

69人阅读 评论(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?

public class Solution {
public int uniquePaths(int m, int n) {
int result = 0;
if(m ==0 || m ==0){
return 0;
}

int[][] nums = new int[m][n];

for(int i = 0; i < n; i++){
nums[0][i] = 1;
}
for(int i = 0; i < m; i++){
nums[i][0] = 1;
}

for(int i = 1; i < m;i++){
for(int j = 1; j < n; j++){
nums[i][j] = nums[i][j-1]+ nums[i-1][j];
}
}

result = nums[m-1][n-1];
return result;
}
}

public class Solution {
public int uniquePaths(int m, int n) {
long result = 1;
if (m == 1 || n == 1) {
return (int)result;
}

int x = m > n ? m : n;
int y = m < n ? m : n;
int chu = 1;

int zonghe = m+n-2;
for(int i =0; i < y-1; i++){
result = result * (zonghe-i);

}
for(int i =0; i < y-1; i++){
chu = chu * (i+1);
}
return (int)(result/chu);
}
}
0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：6952次
• 积分：662
• 等级：
• 排名：千里之外
• 原创：63篇
• 转载：1篇
• 译文：0篇
• 评论：1条
文章分类
阅读排行
最新评论