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 ...
随机推荐
- mongodb的索引操作
在mongodb中,当我们一个集合中的数据量非常大时,比如几百万条数据,如果不使用索引,对数据的查询就会进行全表扫描,这个时候查询的速度就会非常的慢,此时我们就需要为集合建立上索引,从而加快查询的速度 ...
- Noip模拟74 2021.10.11
T1 自然数 考场上当我发现我的做法可能要打线段树的时候,以为自己百分之百是考虑麻烦了 但还是打了,还过掉了所有的样例,于是十分自信的就交了 正解还真是线段树,真就第一题数据结构 但是包括自己造的小样 ...
- 算法:数字推盘游戏--重排九宫(8-puzzle)
一.数字推盘游戏 数字推盘游戏(n-puzzle)是一种最早的滑块类游戏,常见的类型有十五数字推盘游戏和八数字推盘游戏等.也有以图画代替数字的推盘游戏.可能Noyes Palmer Chapman在1 ...
- 基础篇:JAVA集合,面试专用
没啥好说的,在座的各位都是靓仔 List 数组 Vector 向量 Stack 栈 Map 映射字典 Set 集合 Queue 队列 Deque 双向队列 关注公众号,一起交流,微信搜一搜: 潜行前行 ...
- Latex使用CJK包添加字体
最近写论文时有个中文期刊提供的LaTeX模板使用CJK宏包,大致是这样的: \documentclass{article} \usepackage{CJK} \begin{document} \beg ...
- RedHat 7.0 下 FTP 服务的安装,启动,配置,以及虚拟用户的建立
(注意! 区分shell命令和往配置文件里加的代码不同) 一:ftp服务的安装,启动和启用. 1:vim /etc/sysconfig/selinux 改为disabled后重启 ...
- 在代码生成工具Database2Sharp中增加Vue&Element 工作流页面的快速生成
在我们基于框架开发系统的时候,往往对一些应用场景的页面对进行了归纳总结,因此对大多数情况下的页面呈现逻辑都做了清晰的分析,因此在我们基于框架的基础上,增量式开发业务功能的时候,能够事半功倍.代码生成工 ...
- 使用 SSL 加密的 JDBC 连接 SAP HANA 数据库
近期客户为满足安全要求,提了让业务应用使用 SSL 方式连接 SAP HANA 数据库的需求.本人查询 SAP官方文档 发现数据库支持 SSL 连接,有参数直接加到 JDBC 的 URL 后边就行了, ...
- redis如何解决key冲突?
1.业务隔离 不同的业务使用不同的redis集群,或者协议使用redis的不同db. 2.良好的Redis Key的设计 格式:业务标识:系统名称:模块名称:关键词简写 比如:保险:用户管理:用户申请 ...
- Flask搭建弹幕视频网站(1)
说在前面 也不知道最后能不能完成网站,所以就想把这十多天来学习到的点点滴滴记录下来.学的越来越多,所谓全栈也是需要前端基础,越来越感受到压力,但是遇到一个问题就解决一个问题,慢慢习惯之后感觉也还行.说 ...