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

LeetCode 62 — Unique Paths（C++ Java Python）

2014-02-23 22:00:50

leetcode 62. Unique Paths-唯一路径|动态规划

2016-03-11 15:16:22

Unique Paths -- LeetCode

2014-03-26 04:13:48

【LeetCode with Python】 Unique Paths

2014-08-29 18:11:24

LeetCode-62-Unique Paths(动态规划)-Medium

2016-03-14 08:51:58

LeetCode || Unique Paths

2014-04-09 22:17:47

【LeetCode-面试算法经典-Java实现】【062-Unique Paths（唯一路径）】

2015-08-01 07:16:32

leetCode 62.Unique Paths （唯一路径） 解题思路和方法

2015-07-15 14:23:47

leetcode || 62、Unique Paths

2015-04-02 11:24:55

62 Unique Paths

2015-10-14 21:49:20

不良信息举报

[leetcode] 62. Unique Paths