JasonChang

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

use dynamic programming to exchange time with space.

 1 public class Solution {
 2     public int uniquePaths(int m, int n) {
 3         // IMPORTANT: Please reset any member data you declared, as
 4         // the same Solution instance will be reused for each test case.
 5         int[][] mytable = new int[m+1][n+1];
 6         
 7         return getpath(m, n, mytable);
 8         
 9     }
10     
11     public int getpath(int m, int n, int[][] table)
12     {
13         if(m <= 1 || n <= 1)
14             return 1;
15         if(table[m][n] > 0)
16             return table[m][n];
17         table[m][n] = getpath(m-1, n, table) + getpath(m, n-1, table);
18         return table[m][n];
19     }
20 }

 

posted on 2013-11-03 15:00  JasonChang  阅读(146)  评论(0编辑  收藏  举报