JasonChang

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

simple recursion problem

 1 public class Solution {
 2     public int climbStairs(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[] table = new int[n+1];
 6         for(int i = 0; i < n+1; i++)
 7             table[i] = -1;
 8         return climb(table, n);
 9         
10     }
11     private int climb(int[] table, int n)
12     {
13         if(n == 0)
14             return 0;
15         else if(n == 1)
16             return 1;
17         else if(n == 2)
18             return 2;
19         else if(table[n] != -1)
20             return table[n];
21         else{
22             table[n] = climb(table, n-1)+climb(table,n-2);
23             return table[n];
24         }
25     }
26 }

 

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