Climbing Stairs

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 }
原文地址:https://www.cnblogs.com/jasonC/p/3409703.html