1. Climbing Stairs My Submissions QuestionEditorial Solution

    Total Accepted: 106498 Total Submissions: 290003 Difficulty: Easy

    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?

思路:so easy

重要的是思路清晰,对于n阶楼梯,可能由n-1阶迈上来的,也可能由n-2阶楼梯迈上来的,

记f(n)为迈上n阶楼梯所需的步数
f(n)=f(n−1)+f(n−2)
f(2)=2,f(1)=1

就是一斐波纳挈数列,通项和为

an=15√((1+5√2)n−(1−5√2)n)

这里的f(n)=an+1

这里程序有三种实现方式,递归效率最差,自底向上循环其次,最好的是常数级,用公式

class Solution {
public:
int climbStairs(int n) {
vector<int> vec(n+1,0);
vec[1]=1;vec[2]=2;
if(n<=2)return vec[n];
for(int i=3;i<=n;++i)
vec[i]=vec[i-1]+vec[i-2];
return vec[n];
}
};

41-Climbing Stairs-leetcode的更多相关文章

  1. Min Cost Climbing Stairs - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Min Cost Climbing Stairs - LeetCode 注意点 注意边界条件 解法 解法一:这道题也是一道dp题.dp[i]表示爬到第i层 ...

  2. Climbing Stairs - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Climbing Stairs - LeetCode 注意点 注意边界条件 解法 解法一:这道题是一题非常经典的DP题(拥有非常明显的重叠子结构).爬到n ...

  3. climbing stairs leetcode java

    问题描述: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either ...

  4. [LeetCode] Climbing Stairs 爬梯子问题

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  5. Leetcode: climbing stairs

    July 28, 2015 Problem statement: You are climbing a stair case. It takes n steps to reach to the top ...

  6. [LeetCode] Min Cost Climbing Stairs 爬楼梯的最小损失

    On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay ...

  7. [LeetCode] Climbing Stairs (Sequence DP)

    Climbing Stairs https://oj.leetcode.com/problems/climbing-stairs/ You are climbing a stair case. It ...

  8. [LeetCode] 746. Min Cost Climbing Stairs 爬楼梯的最小损失

    On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay ...

  9. [LeetCode] 70. Climbing Stairs 爬楼梯问题

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  10. [LeetCode] 70. Climbing Stairs 爬楼梯

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

随机推荐

  1. [对对子队]会议记录4.19(Scrum Meeting10)

    今天已完成的工作 何瑞 ​ 工作内容:搭建第2关,基本完成第3关 ​ 相关issue:搭建关卡2.3 ​ 相关签入:4.19签入1 4.19签入2 刘子航 ​ 工作内容:完成关卡选择界面的设计图 ​ ...

  2. [技术博客]大闸蟹的技术博客,通过gitlab api进行用户批量创建

    技术博客--通过gitlab api批量注册用户 gitlab登录界面本身提供了register功能,但需要手工一个个添加,对于一次性会添加整个班级的学生的软工平台来说并不科学合理.使用gitlab ...

  3. 2021.9.21考试总结[NOIP模拟58]

    T1 lesson5! 开始以为是个无向图,直接不懂,跳去T2了. 之后有看了一眼发现可暴力,于是有了\(80pts\). 发现这个图是有拓扑序的,于是可以用拓扑排序找最长路径.先找原图内在最长路径上 ...

  4. Verilog设计技巧实例及实现

    Verilog设计技巧实例及实现 1 引言 最近在刷HDLBits的过程中学习了一些Verilog的设计技巧,在这里予以整理.部分操作可能降低代码的可读性和Debug的难度,请大家根据实际情况进行使用 ...

  5. Python AttributeError: module 'string' has no attribute 'atoi'

    python2 中可以用string.atoi 在python3中会报错 替换的方案是 string.atoi(your_str) 替换为 int(your_str) 这个代码python2和pyth ...

  6. hdu 3038 How Many Answers Are Wrong(并查集)

    题意: N和M.有N个数. M个回答:ai, bi, si.代表:sum(ai...bi)=si.如果这个回答和之前的冲突,则这个回答是假的. 问:M个回答中有几个是错误的. 思路: 如果知道sum( ...

  7. linux 内核源代码情景分析——linux 内存管理的基本框架

    386 CPU中的页式存管的基本思路是:通过页面目录和页面表分两个层次实现从线性地址到物理地址的映射.这种映射模式在大多数情况下可以节省页面表所占用的空间.因为大多数进程不会用到整个虚存空间,在虚存空 ...

  8. Linux的inode与block

    1,inode包含文件的元信息,具体来说有以下内容: 文件的字节数 文件拥有者的User ID 文件的Group ID 文件的读.写.执行权限 文件的时间戳,共有三个:ctime指inode上次文件属 ...

  9. 【java+selenium3】特殊元素iframe的定位及详解(三)

    一.iframe 内联框架 1.自己写个网页,仅供理解iframe演示使用,如下 <!DOCTYPE html> <html> <head> <meta ch ...

  10. uni-app nvue页面动态修改导航栏按钮

    话不多说上代码 let pages = getCurrentPages() let page = pages[pages.length - 1]; let currentWebview = page. ...