“The Linux philosophy is “Laugh in the face of danger”.Oops.Wrong One. “Do it yourself”. Yes, that”s it.” Linux的哲学就是“在危险面前放声大笑”,呵呵,不是这句,应该是“一切靠自己,自力更生”才对。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
public class { public int uniquePaths (int m, int n) { int [][] dp = new int [n][m]; for (int i = 0 ; i < n; i++) { for (int j = 0 ; j < m; j++) { if (i == 0 || j == 0 ) dp[i][j] = 1 ; else dp[i][j] = dp[i - 1 ][j] + dp[i][j - 1 ]; System.out.print(dp[i][j] + " " ); } System.out.println(); } return dp[n - 1 ][m - 1 ]; } public int uniquePaths2 (int m, int n) { int [] dp = new int [m]; dp[0 ] = 1 ; for (int i = 0 ; i < n; i++) { for (int j = 1 ; j < m; j++) { dp[j] += dp[j - 1 ]; } } return dp[m - 1 ]; } public static void main (String[] args) { new leetcode62().uniquePaths(7 , 3 ); } }
近期评论