您的位置:首页 > 编程语言 > Java开发

(java)leetcode-70:Climbing Stairs

2017-07-23 20:02 375 查看

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?

Note: Given n will be a positive integer.

解题思路:

这道题大概思路是这样的。第n格梯子,可以是从第n-1格走一格上来,也可以是从第n-2格走两格上来,所以第n格的可能性等于第n-1格的可能性+第n-2格的可能性。

所以这道题是在求斐波那契数列的第n项....

public class Solution {
public int climbStairs(int n) {
int x1 = 0;
int x2 = 1;
for(int i = 0;i<n;i++)
{
int r = x2+x1;
x1 = x2;
x2 = r;
}
return x2;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: