【leetcode】Climbing Stairs (easy)
2024-10-15 17:36:14
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?
跳台阶,之前写过递归的,这次写个非递归的。
到第n层的方式 其实就是 到第n-1层的方式(爬一层)和到第n-2层的方式(爬两层)的和
class Solution {
public:
int climbStairs(int n) {
if(n <= ) return ;
else if(n == ) return ;
else if(n == ) return ;
else
{
int way1 = ;
int way2 = ;
int ans = ;
for(int i = ; i <= n; i++)
{
ans = way1 + way2;
way2 = way1;
way1 = ans;
}
return ans;
}
}
};
最新文章
- Easyui datagrid行内【添加】、【编辑】、【上移】、【下移】
- 数据库 CRUD
- 使用MSBUILD 构建时出错 error MSB3086: Task could not find ";sgen.exe"; using the SdkToolsPath的解决方法
- [转]GLES 3.0 新特性
- bzoj1485:[HNOI2009]有趣的数列
- Preferred Java way to ping a HTTP Url for availability
- jdom学习:读取xml文件
- zookeeper 各节点数据保证是弱一致性
- Fixjs实践——标签、按钮控件
- Android 6.0动态添加权限
- CentOS 7 时区设置
- Shell常见用法小记
- MicrosoftWebInfrastructure 之坑
- How to configure Samba Server share on Debian 9 Stretch Linux
- poj3067 二维偏序树状数组
- 利用Python 脚本生成 .h5 文件 代码
- mybatis一(常用配置信息和获取插入后id)
- ES6简介之let和const命令解说
- 【Devops】【docker】【CI/CD】2.docker启动jenkins环境+安装必要的插件
- Python之CMDB资产管理系统