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. flume的自定义sink-Kafka

    1.创建一个agent,sink类型需指定为自定义sink        vi /usr/local/flume/conf/agent3.conf        agent3.sources=as1  ...

  2. WPF Customize TabControl

    有篇很好的文章 http://www.blogs.intuidev.com/post/2010/01/25/TabControlStyling_PartOne.aspx 详细介绍了如何Customiz ...

  3. C#request 请求响应

    /// <summary> /// 提交POST请求 /// </summary> /// <param name="url">提交地址< ...

  4. Objective-C基础学习笔记——对象初始化

    obj中创建新对象有两种方式:[classname new]和[[classname alloc] init].两种方法等价,Cocoa惯例是使用alloc和init. 1.分配对象: allocat ...

  5. Java学习之List接口

    List接口 List接口的定义如下: public interface List<E>extends Collection<E> 可以发现List接口时Collection接 ...

  6. 使用spring @Scheduled注解运行定时任务、

    曾经框架使用quartz框架运行定时调度问题. 老大说这配置太麻烦.每一个调度都须要多加在spring的配置中. 能不能降低配置的量从而提高开发效率. 近期看了看spring的 scheduled的使 ...

  7. docker iptables 端口映射 nat

    docker  iptables  端口映射  nat #!/bin/bash pro='tcp' NAT_Host='Host_A' NAT_Port=8080 Dst_Host='Host_B' ...

  8. Entrez检索实例 - NCBI

    题目:已知来豆荚斑驳病毒(bean pod mottle virus,BPMV)的名字,查询BPMV基因组信息.核酸序列信息.蛋白序列信息和结构信息 解答: 1.直接搜索,点genome,即可看到病毒 ...

  9. mybatis的缓存机制

    一级缓存: MyBatis的一级缓存指的是在一个Session域内,session为关闭的时候执行的查询会根据SQL为key被缓存(跟mysql缓存一样,修改任何参数的值都会导致缓存失效) packa ...

  10. JAVA泛型解释

    理解Java泛型最简单的方法是把它看成一种便捷语法,能节省你某些Java类型转换(casting)上的操作: 1 List<Apple> box = ...; 2 Apple apple ...