Problem Description
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?
[https://leetcode.com/problems/unique-paths/]
思路
这种题竟然也是M的。。。。。
Code
package q062;
public class Solution {
public int uniquePaths(int m, int n) {
int[][] pathsum=new int[m][n];
pathsum[0][0]=1;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(i-1>=0) pathsum[i][j]+=pathsum[i-1][j];
if(j-1>=0) pathsum[i][j]+=pathsum[i][j-1];
}
}
return pathsum[m-1][n-1];
}
// public static void main(String[] args) {
// Solution s=new Solution();
// System.out.print(s.uniquePaths(1,2));
// }
}