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 ...
随机推荐
- java面试题之----spring与struts2的比较
我们从以下几个维度来区分两者的概念: 1. 机制:spring mvc的入口是servlet,而struts2是filter. 补充几点知识: < Filter 实现javax ...
- SQL 根据身份证号码获取年龄的函数
在数据库的运用过程中,我们时常会碰到根据身份证号码来获取当前的年龄,今天我在这里写了一个函数,就是关于获取年龄的 create or replace function FUNC_COMPARE_SFZ ...
- February 25 2017 Week 8 Saturday
Energy and persistence can conquer all things. 能量和毅力可以征服一切. I have the persistence, but it seems I a ...
- 如何读写json文件
代码如下: import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io. ...
- 如何在Chrome development tool里查看C4C前台发送的请求细节
我们可以在Chrome development tool的network tab里观察到从前台UI发送到后台的HTTP请求: 更多Chrome Development Tool的使用工具请查看我的博客 ...
- 大小端,"字节序"
2 字节序 2.1 字节 字节(Byte)作为计算机世界的计量单位,和大家手中的人民币多少多少“元”一个意思.反正,到了计算机的世界,说字节就对了,使用人家的基本计量单位,这是入乡随俗. 比如,一个电 ...
- 课堂笔记:HTML----------图片热点
HTML----------图片热点: 规划出图片上的一个区域,可以做出超链接,直接点击图片区域就可完成跳转的效果. 代码: <!DOCTYPE html PUBLIC "-//W3C ...
- [19/03/16-星期六] 常用类_Date时间类&DateFormat类
一.Date时间类 计算机中 以1970 年 1 月 1 日 00:00:00定为基准时间,每个度量单位是毫秒(1秒的千分之一) 用ong类型的变量来表示时间,如当前时刻数值:long now =n ...
- 推荐一个zookeeper信息查看工具
zookeeper信息查看工具 下载地址:https://issues.apache.org/jira/secure/attachment/12436620/ZooInspector.zip 解压,打 ...
- jenkins 安装配置: centos-master windows/linux-slave + nginx代理 + node + job
centos install jenkins: 1.sudo vi /etc/yum.repos.d/jenkins.repo [jenkins] name=Jenkins baseurl=http: ...