41-Climbing Stairs-leetcode
- 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)=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的更多相关文章
- Min Cost Climbing Stairs - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Min Cost Climbing Stairs - LeetCode 注意点 注意边界条件 解法 解法一:这道题也是一道dp题.dp[i]表示爬到第i层 ...
- Climbing Stairs - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Climbing Stairs - LeetCode 注意点 注意边界条件 解法 解法一:这道题是一题非常经典的DP题(拥有非常明显的重叠子结构).爬到n ...
- climbing stairs leetcode java
问题描述: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either ...
- [LeetCode] Climbing Stairs 爬梯子问题
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- Leetcode: climbing stairs
July 28, 2015 Problem statement: You are climbing a stair case. It takes n steps to reach to the top ...
- [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 ...
- [LeetCode] Climbing Stairs (Sequence DP)
Climbing Stairs https://oj.leetcode.com/problems/climbing-stairs/ You are climbing a stair case. It ...
- [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 ...
- [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 ...
- [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 ...
随机推荐
- CSP-S 2021 遗言
感谢€€£,谢谢宁嘞! 第一题,€€£给了很多限制条件,什么"先到先得"."只有一个跑道",让它看起来很好做,然后来骗,来偷袭,广大"消费者" ...
- TVS管性能及选型总结
https://wenku.baidu.com/view/5b5bda5526fff705cc170af8.html
- matplotlib散点图
我们常用的统计图如下: 1.学会绘制散点图 一个小demo: 假设通过爬虫你获取到了北京2016年3,10月份每天白天的最高气温(分别位于列表a,b),那么此时如何寻找出气温和随时间(天)变化的某种规 ...
- 如何优雅的处理 accept 出现 EMFILE 的问题
通常情况下,服务端调用 accept 函数会返回一个新的文件描述符,用于和客户端之间的数据传输 在服务器的开发中,有时会遇到这种情况:当调用 accept 函数接受客户端连接,函数返回失败,对应的错误 ...
- CPU使用率和平均负载
转载: https://mp.weixin.qq.com/s?__biz=MzU4NzU0MDIzOQ==&mid=2247487782&idx=3&sn=3f04bb053d ...
- AXI总线简介、ID分析、DMA、Vivado烧录、系统集成
转载:https://blog.csdn.net/CrazyUncle/article/details/89918030?depth_1-utm_source=distribute.pc_releva ...
- C#笔记1__命名空间 / 常量 / object / is、as、...?... :...
命名空间:namespace Test1{ ... } 引用命名空间:using System; using 别名=命名空间 常量:const double PI=3.14; using System ...
- Java线程的三种实现方法
Java多线程详解 线程简介 多任务,多线程 多任务情况中,虽然可以完成,但是实际上,多任务的完成是由一个一个小任务的完成来实现的,也就是说在执行多任务时,不是同时执行多个任务,而是一个时间段内只完成 ...
- HTML 罗盘式时钟
代码块: 1 <!DOCTYPE html> 2 <html lang="zh-hans"> 3 <head> 4 <meta chars ...
- diff 命令,防止遗忘
常规输出: diff 1.file 2.file 并排格式输出: diff 1.file 2.file -y -W 50 显示说明 "|"表示前后2个文件内容有不同 "& ...