Last active
January 2, 2016 13:39
-
-
Save onewaterdrop/8311113 to your computer and use it in GitHub Desktop.
UniquePaths for leetcode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class UniquePaths { | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
// TODO Auto-generated method stub | |
System.out.println(uniquePaths2(44,40)); | |
} | |
public static int uniquePaths(int m, int n) { | |
if(m==1 || n==1)return 1; | |
else{ | |
return uniquePaths(m-1,n) + uniquePaths(m,n-1); | |
} | |
} | |
public static int uniquePaths2(int m, int n) { | |
if(m==1 || n==1)return 1; | |
int[][] A= new int[m+1][n+1]; | |
java.util.Arrays.fill(A[1],1); | |
for(int j=1;j<=m;j++){ | |
A[j][1]=1; | |
} | |
for(int i=2;i<=m;i++){ | |
for(int j=2;j<=n;j++){ | |
A[i][j]=A[i-1][j]+A[i][j-1]; | |
System.out.println(A[i][j] + ","); | |
} | |
} | |
return A[m][n]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment