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 ...
随机推荐
- CentOS系统优化一键配置脚本
#!/usr/bin/env bash PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export P ...
- 震惊,本Orzer下阶段直接怒送四个笑脸
众所周知,在hzoi帝国中,Wzx是最菜的.那么究竟有多菜呢?下面就和小编一起来看看吧. 近日,hzoi最菜的wzx在第四阶段竟然怒送4个笑脸,同机房神犇直呼wzx太菜了! 以上就是wzx第四阶段怒送 ...
- 为什么用于开关电源的开关管一般用MOS管而不是三极管
区别: 1.MOS管损耗比三极管小,导通后压降理论上为0. 2.MOS管为电压驱动型,只需要给电压即可,意思是即便串入一个100K的电阻,只要电压够,MOS管还是能够导通. 3.MOS管的温度特性要比 ...
- 嵌入式物联网之SPI接口原理与配置
本实验采用W25Q64芯片 W25Q64是华邦公司推出的大容量SPI FLASH产品,其容量为64Mb.该25Q系列的器件在灵活性和性能方面远远超过普通的串行闪存器件.W25Q64将8M字节的容量分为 ...
- Python gpu 显卡小工具 gpu
安装 pip install gpustat 或者 换源 pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --upgrade gpust ...
- hdu 4521 小明序列(线段树,DP思想)
题意: ①首先定义S为一个有序序列,S={ A1 , A2 , A3 , ... , An },n为元素个数 : ②然后定义Sub为S中取出的一个子序列,Sub={ Ai1 , Ai2 , Ai3 , ...
- 【Azure 应用服务】App Service for Linux 中实现 WebSocket 功能 (Python SocketIO)
问题描述 使用 python websockets 模块作为Socket的服务端,发布到App Service for Linux环境后,发现Docker Container无法启动.错误消息为: 2 ...
- 使用google zxing生成二维码图片
生成二维码工具类: 1 import java.awt.geom.AffineTransform; 2 import java.awt.image.AffineTransformOp; 3 impor ...
- Iptables 代理局域网内的主机上网
参考连接:https://developer.aliyun.com/article/607330 环境为云环境,有外网主机的IP为192.168.0.39,无外网主机的IP为192.168.0.228 ...
- /etc/hosts 详解
/etc/hosts:主机名查询静态表,是ip地址与域名快速解析的文件.ip地址与主机名之间的映射,包括主机的别名. 通常将常用的域名和ip地址映射加入到hosts文件中,实现快速方便的访问. 如果没 ...