70. Climbing Stairs(LeetCode)

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?

Note: Given n will be a positive integer.

 1 class Solution {
 2 public:
 3     int climbStairs(int n) {
 4       int sum = 0;
 5         if (n <= 1)
 6             return 1;
 7         if (n == 2)
 8             return 2;
 9         int a = 1, b = 2;
10         for (int i = 3; i <= n; i++)
11         {
12             sum = a + b;
13             a = b;
14             b = sum;
15             sum=0;
16         }
17         return b;
18     }
19 };
原文地址:https://www.cnblogs.com/wujufengyun/p/6894463.html