Climbing Stairs

Q:

You are climbing a stair case. It takes n steps to reach to the top.

Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

 

A:

F(n) = F(n - 1) + F(n - 2)

写了3个解决办法,solution1显然会超时,因为有大量的重复计算,solution3是对它的改进,代码相当的简单。solution2是利用了传说中的fibonacci数列,fibonacci是个变态吧,还有那fibonacci堆神马的,真tm烦人!

按说fibonacci数列的速度会达到logn,测试的速度跟solution3不相上下,想来如果n比较大的话solution2会好使一点吧。

ok,就这样。

 1 class Solution1 {
 2  public:
 3   int climbStairs(int n) {
 4     // Start typing your C/C++ solution below
 5     // DO NOT write int main() function
 6     if (n <= 0) return 0;
 7     if (n == 1) return 1;
 8     if (n == 2) return 2;
 9     return climbStairs(n - 1) + climbStairs(n - 2);
10   }
11 };
12 
13 class Solution2 {
14  public:
15   int climbStairs(int n) {
16     // Start typing your C/C++ solution below
17     // DO NOT write int main() function
18     if (n <= 0) return 0;
19     if (n == 1) return 1;
20     if (n == 2) return 2;
21     vector<vector<int> > org_matrix_a;
22     vector<int> row;
23     row.push_back(1);
24     row.push_back(1);
25     org_matrix_a.push_back(row);
26     row[1] = 0;
27     org_matrix_a.push_back(row);
28     vector<vector<int> > org_matrix_b;
29     row.resize(1);
30     row[0] = 2;
31     org_matrix_b.push_back(row);
32     row[0] = 1;
33     org_matrix_b.push_back(row);
34     vector<vector<int> > final_matrix =
35       matrixMultiplication(matrixPower(org_matrix_a, n - 2), org_matrix_b);
36     return final_matrix[0][0];
37   }
38 
39  private:
40   vector<vector<int> > matrixPower(const vector<vector<int> >& matrix, int n) {
41     vector<vector<int> > new_matrix;
42     if (n <= 0) {
43       return new_matrix;
44     }
45     if (n == 1) return matrix;
46     new_matrix = matrixMultiplication(matrix, matrix);
47     if (n%2 == 0) return matrixPower(new_matrix, n/2);
48     return matrixMultiplication(matrixPower(new_matrix, n/2), matrix);
49   }
50 
51   vector<vector<int> > matrixMultiplication(
52       const vector<vector<int> >& matrix_a, const vector<vector<int> >& matrix_b) {
53     int m = matrix_a.size();
54     int n = matrix_a[0].size();
55     int s = matrix_b[0].size();
56     vector<vector<int> > matrix_res;
57     for (int i = 0; i < m; ++i) {
58       vector<int> row;
59       for (int j = 0; j < s; ++j) {
60         int tmp = 0;
61         for (int k = 0; k < n; ++k) {
62           tmp += matrix_a[i][k]*matrix_b[k][j];
63         }
64         row.push_back(tmp);
65       }
66       matrix_res.push_back(row);
67     }
68     return matrix_res;
69   }
70 };
71 
72 class Solution3 {
73  public:
74   int climbStairs(int n) {
75     // Start typing your C/C++ solution below
76     // DO NOT write int main() function
77     if (n <= 0) return 0;
78     if (n == 1) return 1;
79     if (n == 2) return 2;
80     int pre_one = 2;
81     int pre_two = 1;
82     int time = n - 2;
83     while (time-- > 0) {
84       int tmp = pre_two;
85       pre_two = pre_one;
86       pre_one = tmp + pre_two;
87     }
88     return pre_one;
89   }
90 };

 

posted @ 2013-06-17 12:22  dmthinker  阅读(99)  评论(0)    收藏  举报