leetcode:Climbing Stairs

来源:互联网 发布:unix内核源码剖析 编辑:程序博客网 时间:2024/04/27 16:26

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?

这个其实就是斐波那契数列,题目描述有n个楼梯,每次只能爬一阶或两阶,n阶楼梯总共有几种爬法,

这个是简单版的,更难的是每次都可以爬 1—n阶楼梯

class Solution {public:    int climbStairs(int n) {        if (n<=0)return 0;if(n==1)return 1;if (n==2)return 2;int one = 1;int two = 2;int sum = 0;for (int i = 2; i < n; i++){sum = one + two;one = two;two = sum;//or  // two += one; // one = two - one;//return two;}return sum;    }};






0 0