• Leetcode 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?

    熟悉斐波那契数列的一看就知道斐波那契数列是0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ...

    其通项是f[k] = f[k-1]+f[k-2],k>=2

    其中f[0] = 0, f[1] = 1;

    而此题类似斐波那契数但初始值有点不同

    f[0] = 0, f[1]= 1, f[2] = 2, 对其迭代就行

    int climbStairs(int n){
        if(n == 0 || n == 1 || n == 2) return n;
        int stepOne = 1, stepTwo = 2, result = 0;
        for(int i = 3; i <= n ; ++ i){
            result = stepOne + stepTwo;
            stepOne = stepTwo;
            stepTwo = result;
        }
        return result;
    }
  • 相关阅读:
    Servlet
    反射
    Python之装饰器
    app——升级测试点
    Python之基本运算符
    HTTP常见的几种认证机制
    Shell文件包含
    Shell输入/输出重定向
    Shell函数
    Shell循环语句
  • 原文地址:https://www.cnblogs.com/xiongqiangcs/p/3803156.html
Copyright © 2020-2023  润新知