ou are climbing a stair case. It takes n steps to reach to the top.

Each time you can either climb  or  steps. In how many distinct ways can you climb to the top?

分析:类似斐波那契序列,使用动态规划的思想。定义f(n)为台阶数为n时青蛙跳到台阶顶部的方法数。那么当n>2 时f(n) = f(n-1) + f(n-2)    f(1) = 1; f(2) = 2;

class Solution {
public:
int climbStairs(int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(n <= ) return n;
int f1 = , f2 = , res = ;
for(int i = ; i <= n; ++i){
res = f1 + f2;
f1 = f2;
f2 = res;
}
return res;
}
};

LeetCode_Climbing Stairs的更多相关文章

  1. [LeetCode] Climbing Stairs 爬梯子问题

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  2. [LintCode] Climbing Stairs 爬梯子问题

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  3. Leetcode: climbing stairs

    July 28, 2015 Problem statement: You are climbing a stair case. It takes n steps to reach to the top ...

  4. LintCode Climbing Stairs

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  5. 54. Search a 2D Matrix && Climbing Stairs (Easy)

    Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...

  6. Climbing Stairs

    Climbing Stairs https://leetcode.com/problems/climbing-stairs/ You are climbing a stair case. It tak ...

  7. 3月3日(6) Climbing Stairs

    原题 Climbing Stairs 求斐波那契数列的第N项,开始想用通项公式求解,其实一个O(n)就搞定了. class Solution { public: int climbStairs(int ...

  8. Cllimbing Stairs [LeetCode 70]

    1- 问题描述 You are climbing a stair case. It takes n steps to reach to the top. Each time you can eithe ...

  9. leetCode 70.Climbing Stairs (爬楼梯) 解题思路和方法

    Climbing Stairs  You are climbing a stair case. It takes n steps to reach to the top. Each time you ...

随机推荐

  1. CH Round #54 - Streaming #5 (NOIP模拟赛Day1)

    A.珠 题目:http://ch.ezoj.tk/contest/CH%20Round%20%2354%20-%20Streaming%20%235%20(NOIP模拟赛Day1)/珠 题解:sb题, ...

  2. 有关autoresizingMask属性遇到的一个小问题

    前言:在讲述这个小问题之前,我们有必要先了解一下UIViewAutoresizing的有关属性概念和使用详解. 参考:自动布局之autoresizingMask使用详解(Storyboard& ...

  3. animate.min.css 动画样式移动端存在的问题

    使用animate.min.css可以使用很多动画效果,包括3D效果,现在也可以应用于HTML5手机移动端,使用切换效果的时候会导致页面出现卡顿现象,可以使用css3 transform 方法硬件加速 ...

  4. iOS--tableview分组

    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ ; } -(NSInteger)tableView:(UITable ...

  5. 最近看到一篇cell点击时的动画,感觉还不错

    http://blog.csdn.net/cloudox_/article/details/51262827

  6. DedeCMS安装及目录结构

    一.安装DedeCMS 1.下载DedeCMS安装包,我下载的版本是DedeCMS-V5.7-UTF8-SP1.tar.gz 官方下载地址 2.解压DedeCMS-V5.7-UTF8-SP1.tar. ...

  7. hibernate 一对多双向关联 详解

    一.解析: 1.  一对多双向关联也就是说,在加载班级时,能够知道这个班级所有的学生. 同时,在加载学生时,也能够知道这个学生所在的班级. 2.我们知道,一对多关联映射和多对一关联映射是一样的,都是在 ...

  8. 转载:mybatis和hibernate 解析

    第一章     Hibernate与MyBatis Hibernate 是当前最流行的O/R mapping框架,它出身于sf.net,现在已经成为Jboss的一部分. Mybatis 是另外一种优秀 ...

  9. Matplotlib下载地址

    http://www.lfd.uci.edu/~gohlke/pythonlibs/#matplotlib

  10. [Javascript] Array methods in depth - indexOf

    indexOf is used to search for a value or reference inside of an array. In this lesson we first look ...