LeetCode: Climbing Stairs

一开始用dfs结果large超时,马上就想到dp了。。哎,下次先得去想dp啊。。

 1 class Solution {

 2 public:

 3     int climbStairs(int n) {

 4         // Start typing your C/C++ solution below

 5         // DO NOT write int main() function

 6         vector<int> f(n+1, 0);

 7         f[0] = 1, f[1] = 1;

 8         for (int i = 2; i <= n; i++)

 9             f[i] = f[i-1] + f[i-2];

10         return f[n];

11     }

12 };

 C#:

 1 public class Solution {

 2     public int ClimbStairs(int n) {

 3         int[] f = new int[n+1];

 4         f[0] = 1;

 5         f[1] = 1;

 6         for (int i = 2; i <= n; i++) {

 7             f[i] = f[i-1] + f[i-2];

 8         }

 9         return f[n];

10     }

11 }
View Code

 

你可能感兴趣的:(LeetCode)