Climbing Stairs @leetcode

http://oj.leetcode.com/problems/climbing-stairs/

不要在一个CASE里重复计算

 1 int res[10000]={0};
 2 class Solution {
 3 public:
 4     int climbStairs(int n) {
 5         // IMPORTANT: Please reset any member data you declared, as
 6         // the same Solution instance will be reused for each test case.
 7         if(n <= 0)
 8             return 0;
 9         if(n == 1)
10             return 1;
11         if(n == 2)
12             return 2;
13         if(n == 3)
14             return 3;
15         if(n<10000&&res[n]!=0)
16             return res[n];
17         if(n-1<10000)
18         {
19             res[n-1] = climbStairs(n-1);
20         }
21         if(n-2<10000)
22         {
23             res[n-2] = climbStairs(n-2);
24         }
25         return res[n-1]+res[n-2];
26     }
27 };

 

posted @ 2013-11-20 00:03  沙茶面  阅读(162)  评论(0编辑  收藏  举报