classSolution{publicintuniquePaths(int m,int n){int[][] grid =newint[m][n];// Start point
grid[0][0]=0;// Fill in initial value 1for(int col =0; col < n;++col){
grid[0][col]=1;}for(int row =0; row < m;++row){
grid[row][0]=1;}for(int row =1; row < m;++row){for(int col =1; col < n;++col){// 上加左一格
grid[row][col]= grid[row -1][col]+ grid[row][col -1];}}// 終點return grid[m -1][n -1];}}
bottom-up
classSolution{publicintuniquePaths(int m,int n){// result rowint[] row =newint[n];// bottom row is with only 1 possibility of each itemArrays.fill(row,1);// Iterate through all rows, except bottomfor(int i =0; i < m -1;++i){int[] newRow =newint[n];Arrays.fill(newRow,1);// Iterate through the items from right to left// Except the right most itemfor(int j = n -2; j >=0;--j){// Right plus down
newRow[j]= newRow[j +1]+ row[j];}// For next round cal.
row = newRow;}return row[0];}}