Advertisement
gelita

Unique Paths

Mar 9th, 2020
499
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 0.48 KB | None | 0 0
  1. class Solution {
  2.     public int uniquePaths(int m, int n) {
  3.       if(m== 0 || n == 0){
  4.         return 1;
  5.       }
  6.       if(m < 0 || n < 0){
  7.         return 0;
  8.       }
  9.       int[][] cache = new int[m][n];
  10.       for(int i = 0; i<m; i++){
  11.           for(int j = 0; j<n; j++){
  12.             if(i == 0 || j ==0){
  13.                 cache[i][j]= 1;
  14.             }else{
  15.                 cache[i][j] = cache[i-1][j] + cache[i][j-1];
  16.             }
  17.       }
  18.     }
  19.     return cache[m-1][n-1];
  20.     }
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement