/*
* @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. C语言实现一元多项式求积

    #include <stdio.h>#include <stdlib.h>#include <math.h>typedef struct Node{    int ...

  2. js中top、clientTop、scrollTop、offsetTop的区别 文字详细说明版

    网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可见区域宽: document.body.offset ...

  3. ms17_010利用复现(32位)

    准备阶段: 1,原版windows7:cn_windows_7_enterprise_x86_dvd_x15-70737.iso 2,kali系统,  虚拟机 3,用于32位机的攻击模块:Eterna ...

  4. Python—面向对象 封装03

    接着上面的一篇继续往下: 如何隐藏 在python中用双下划线开头的方式将属性隐藏起来(设置成私有的) class A: __x = 1 # _A__x = 1 def __init__(self, ...

  5. Unity让带有Rigidbody组件的游戏对象停止运动

    Rigidbody rigidbody = transform.GetComponent<Rigidbody>(); rigidbody.velocity = Vector3.zero; ...

  6. <body> 中的 JavaScript 函数

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  7. papers-06-02

    午睡被同事吵醒,只好干活.看到微信公众号有一篇文章说老朋友呢,点进去发现原来相关的工作好多,而且好新好细致. 微信的文章可以见这里: 探究最陌生的老朋友Softmax 里面的几篇文章可以看看. Lar ...

  8. JavaScript 笔记总结

    一.js的简介  1.js是什么 js是可以嵌入到html中,是 基于对象 和 事件驱动 的 脚本语言 特点: (1)交互性 (2)安全性:js不能访问本地磁盘 (3)跨平台:浏览器中都具备js解析器 ...

  9. Java数据结构的实现

    1.基于数组的链表 package array; import java.util.Arrays; /** * 基于数组的链表 * * @author 王彪 * */ public class MyA ...

  10. python语言验证码识别,以后不用老输入验证码了。

    1.Python 3.6 安装包 1.要加环境变量 2.pip安装PIL库 3.pip安装pytesseract模块 2.tesseract-ocr-setup-4.00.00dev.exe   -- ...