[leedcode 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?

public class Solution {
    public int climbStairs(int n) {
        //f(n)=f(n-1)+f(n-2);DP
        
        int f1=1;
        int f2=2;
        /*for(int i=3;i<=n;i=i+2){
            f1=f1+f2;
            f2=f1+f2;
        }
        if(n%2==0) return f2;
        else return f1;*/
        // 解法二:
        if(n==1) return 1;
        if(n==2) return 2;
        for(int i=3;i<=n;i++){
            int f3=f1+f2;
            f1=f2;
            f2=f3;
            
        }
        return f2;
    }
}

 

posted @ 2015-07-13 22:22  ~每天进步一点点~  阅读(105)  评论(0编辑  收藏  举报