Leecode刷题之旅-C语言/python-70爬楼梯
/*
* @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爬楼梯的更多相关文章
- Leecode刷题之旅-C语言/python-1.两数之和
开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...
- Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符
/* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...
- Leecode刷题之旅-C语言/python-28.实现strstr()
/* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...
- Leecode刷题之旅-C语言/python-7.整数反转
/* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...
- Leecode刷题之旅-C语言/python-434 字符串中的单词数
/* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...
- Leecode刷题之旅-C语言/python-326 3的幂
/* * @lc app=leetcode.cn id=326 lang=c * * [326] 3的幂 * * https://leetcode-cn.com/problems/power-of-t ...
- Leecode刷题之旅-C语言/python-263丑数
/* * @lc app=leetcode.cn id=263 lang=c * * [263] 丑数 * * https://leetcode-cn.com/problems/ugly-number ...
- Leecode刷题之旅-C语言/python-383赎金信
/* * @lc app=leetcode.cn id=383 lang=c * * [383] 赎金信 * * https://leetcode-cn.com/problems/ransom-not ...
- Leecode刷题之旅-C语言/python-349两整数之和
/* * @lc app=leetcode.cn id=371 lang=c * * [371] 两整数之和 * * https://leetcode-cn.com/problems/sum-of-t ...
随机推荐
- Android应用开发基础之五:网络编程(二)
HttpClient 发送get请求 创建一个客户端对象 HttpClient client = new DefaultHttpClient(); 创建一个get请求对象 HttpGet hg = n ...
- C# 64位win7下DllImport LoadLibrary函数失败 z
[DllImport["kernel32.dll"]] static extern IntPtr LoadLibrary(string lpFileName); public vo ...
- 在centos7中安装redis,并通过node.js操作redis
引言 最近在学习node.js 连接redis的模块,所以尝试了一下在虚拟机中安装cent OS7,并安装redis,并使用node.js 操作redis.所以顺便做个笔记. 如有不对的地方,欢迎大家 ...
- sublime text3 英文版转为中文版
第一步设置好:https://packagecontrol.io/installation#st3 简单几步 : 1. 点击菜单栏中“preferences”,弹出选项中找到“package cont ...
- 广义mandelbrot集,使用python的matplotlib绘制,支持放大缩小
迭代公式的指数,使用的1+5j,这是个复数.所以是广义mandelbrot集,大家能够自行改动指数,得到其它图形.各种库安装不全的,自行想办法,能够在这个站点找到差点儿全部的python库 http: ...
- Multi-Thread 1: how to use synchronized
1. synchronized If two threads are using the same function( here we use output to print out string) ...
- 郑州集训Day4 [小Cat与小鲜肉]
考试的时候由于没有想出这道题就弃疗了 发现主要还是自己姿势不够 [问题描述] \(P\) 校某宿舍人才辈出,其舍长图书馆男神因被偷拍侧身照而在网络上一票走红. 小鲜肉 \(SJY\) 是小 \(Cat ...
- 五、设置 IntelliJ IDEA 主题和字体的方法
我们已经用 IntelliJ IDEA 创建了第一个 Java 项目 HelloWorld,如下图所示: 观察上图,大家有没有发现一些问题,例如,整个界面的字体是不是都太小了一点啊?不知道大家感受如何 ...
- 学大伟业 Day 2 培训总结
一.dp 动态规划的本质 是一种思想.通过对原问题划分成子问题,寻找子问题之间的联系,通过求解子问题得出原问题的解.与贪心不同的是,动归是深谋远虑,考虑全局最优解:而贪心则目光短浅,只考虑局部最优解. ...
- MFC通过URL下载并保存文件代码 转载
http://blog.csdn.net/charlessimonyi/article/details/8666108?utm_source=tuicool&utm_medium=referr ...