leetcode——70——Climbing Stairs

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?

 相当于求Fabonacci数列

class Solution {
public:
    int climbStairs(int n) {
        
        int stepone = 0;  
        int steptwo = 1;  
        int sum = 0;  
          
        for(int i = 0;i<n;i++)  
        {  
            sum = stepone + steptwo;  
            stepone = steptwo;  
            steptwo = sum;  
              
              
        }  
          
        return sum;  

    }
};


你可能感兴趣的:(LeetCode,算法题)