/*
* @lc app=leetcode.cn id=70 lang=c
*
* [70] 爬楼梯
*
* https://leetcode-cn.com/problems/climbing-stairs/description/
*
* algorithms
* Easy (44.44%)
* Total Accepted: 33.5K
* Total Submissions: 75.4K
* Testcase Example: '2'
*
* 假设你正在爬楼梯。需要 n 阶你才能到达楼顶。
*
* 每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?
*
* 注意:给定 n 是一个正整数。
*
* 示例 1:
*
* 输入: 2
* 输出: 2
* 解释: 有两种方法可以爬到楼顶。
* 1. 1 阶 + 1 阶
* 2. 2 阶
*
* 示例 2:
*
* 输入: 3
* 输出: 3
* 解释: 有三种方法可以爬到楼顶。
* 1. 1 阶 + 1 阶 + 1 阶
* 2. 1 阶 + 2 阶
* 3. 2 阶 + 1 阶
*
*
*/
int climbStairs(int n) {
int f1 = ;
int f2 = ;
int f3;
int i =;
if(n==){
return ;
}
if(n<){
return ;
}
while(i<n){
f3 = f1+f2;
f1 = f2;
f2 = f3;
i++;
}
return f3;
}

n=1的时候值为1,n=2的时候值为2。n等于3的时候值为3,f3=f1+f2 是典型的斐波那契数列

所以求n阶需要多少步,其实就是求f(n)的过程。

------------------------------------------------------------------------------------------------------------------------

python:

#
# @lc app=leetcode.cn id=70 lang=python3
#
# [70] 爬楼梯
#
# https://leetcode-cn.com/problems/climbing-stairs/description/
#
# algorithms
# Easy (44.44%)
# Total Accepted: 33.5K
# Total Submissions: 75.4K
# Testcase Example: '2'
#
# 假设你正在爬楼梯。需要 n 阶你才能到达楼顶。
#
# 每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?
#
# 注意:给定 n 是一个正整数。
#
# 示例 1:
#
# 输入: 2
# 输出: 2
# 解释: 有两种方法可以爬到楼顶。
# 1. 1 阶 + 1 阶
# 2. 2 阶
#
# 示例 2:
#
# 输入: 3
# 输出: 3
# 解释: 有三种方法可以爬到楼顶。
# 1. 1 阶 + 1 阶 + 1 阶
# 2. 1 阶 + 2 阶
# 3. 2 阶 + 1 阶
#
#
#
class Solution:
def climbStairs(self, n: int) -> int:
f1=1
f2=2
f3=0
i=2
if n==1:
return 1
if n==2:
return 2
if n<1:
return 0
while i<n:
f3 = f1+f2
f1 = f2
f2 = f3
i+=1
return f3

Leecode刷题之旅-C语言/python-70爬楼梯的更多相关文章

  1. Leecode刷题之旅-C语言/python-1.两数之和

    开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...

  2. Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符

    /* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...

  3. Leecode刷题之旅-C语言/python-28.实现strstr()

    /* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...

  4. Leecode刷题之旅-C语言/python-7.整数反转

    /* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...

  5. Leecode刷题之旅-C语言/python-434 字符串中的单词数

    /* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...

  6. Leecode刷题之旅-C语言/python-326 3的幂

    /* * @lc app=leetcode.cn id=326 lang=c * * [326] 3的幂 * * https://leetcode-cn.com/problems/power-of-t ...

  7. Leecode刷题之旅-C语言/python-263丑数

    /* * @lc app=leetcode.cn id=263 lang=c * * [263] 丑数 * * https://leetcode-cn.com/problems/ugly-number ...

  8. Leecode刷题之旅-C语言/python-383赎金信

    /* * @lc app=leetcode.cn id=383 lang=c * * [383] 赎金信 * * https://leetcode-cn.com/problems/ransom-not ...

  9. Leecode刷题之旅-C语言/python-349两整数之和

    /* * @lc app=leetcode.cn id=371 lang=c * * [371] 两整数之和 * * https://leetcode-cn.com/problems/sum-of-t ...

随机推荐

  1. MySQL中AddDate函数的疑惑

    无论使用哪一种RDBMS,都需要使用到其中的一些日期转换函数,在使用MySQL的AddDate函数时,遇到了点小问题,稍作记录. root@localhost:mysql3376.sock [(non ...

  2. Shell脚本批量修改图片尺寸

    #!/bin/sh function scandir(){ local cur_dir parent_dir workdir workdir=$ cd ${workdir} if [ ${workdi ...

  3. GO Lang学习笔记 - 基础知识

    Go lang Learn Note 标签(空格分隔): Go Go安装和Go目录 设置环境变量GOROOT和GOPATH,前者是go的安装目录,后者是开发工作目录.go get包只会将包下载到第一个 ...

  4. delphi7 打开project/options 出错

    出错提示:Access violation at address 0012F88F. Write of address 0012F88F.然后又提示一条:Access violation at add ...

  5. 第3次Scrum冲刺

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  6. C语言 数组的使用

    #include <stdio.h> // 数组的定义和存储 void test1() { ]; // 64bit环境下占用4*5=20个字节 // 计算数组占据的存储空间 // size ...

  7. RHEL6.5和RHEL7 的区别(转)

    Rhel6.5实验环境搭建 1)操作系统安装 RHEL7是一站式安装   2)网卡配置文件 RHEL6: /etc/sysconfig/network-scripts/ifcfg-eth0 RHEL7 ...

  8. php 的 number_format使用

    $num = 1.0258963147; // 1.0259 $num = number_format($num, 4, '.', ''); $num = number_format($num); $ ...

  9. [转]CUDA和OpenGL互操作的实现及分析

    CUDA和OpenGL互操作的实现及分析刘进锋.郭雷(西北工业大学 自动化学院,陕西西安710129) 1 CUDA与OpenGL概述 OpenGL是图形硬件的软件接口,它是在SGI等多家世界著名的计 ...

  10. 【JeeSite】角色分配

    主要是(roleAssign.jsp , selectUserToRole.jsp )2个jsp页面的JS方法调用比较复杂,主页面要获取弹窗页面的数据 var pre_ids = h.find(&qu ...