LeetCode 70. Climbing Stairs

来源:互联网 发布:php解析json数组 编辑:程序博客网 时间:2024/05/22 06:43

题目:
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阶台阶的方法就是一个fabonacci数列的第n个值,因为满足d[i]=d[i-1]+d[i-2]

代码:

class Solution {public:    int climbStairs(int n) {        int a = 1;//刚开始跳一阶台阶有一种方法        int b = 1;//刚开始跳二阶台阶有一种方法        while (n--){//其实这个就是求fabonacci的第n个数            b += a;            a = b - a;        }        return a;    }};

输出结果: 0ms

原创粉丝点击