LeetCode Climbing Stairs

来源:互联网 发布:淘宝 上下架时间 编辑:程序博客网 时间:2024/06/11 06:10

题目

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?

 

到第i格的路线数n(i)=n(i-1)+n(i-2),由此递推即可。

 

代码:

class Solution {public:    int climbStairs(int n) {        if(n<3)return n;int nw1=1,nw2=2,nw;//到当前格前两格的路线数,前一个的路线数,当前格的路线数for(int i=3;i<=n;i++){nw=nw1+nw2;nw1=nw2;nw2=nw;}return nw;    }};


 

 

 

 

0 0
原创粉丝点击