LeetCode | Climbing Stairs(爬楼梯)
来源:互联网 发布:linux批量管理工具 编辑:程序博客网 时间:2024/06/06 01:04
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来。a[i] = a[i-1] + a[i-2]。本来想设一个数组,但由于只涉及到两个数之间的求和,并且是相邻的两个数,就没必要去设数组了,只用两个变量即可。
class Solution {public: int climbStairs(int n) { if(n == 1 || n== 2) return n; int temp1 = 1; int temp2 = 2; for(int i = 3;i <= n;i++){ int temp = temp1+temp2; temp1 = temp2; temp2 = temp; } return temp2; }};
0 0
- LeetCode | Climbing Stairs(爬楼梯)
- LeetCode-Climbing Stairs(爬楼梯问题)
- LeetCode 70. Climbing Stairs(爬楼梯)
- LeetCode-Climbing Stairs(爬楼梯问题)
- Climbing Stairs 爬楼梯 @LeetCode
- Leetcode Climbing Stairs 爬楼梯
- LeetCode--Climbing Stairs(爬楼梯)
- Climbing Stairs(爬楼梯)
- LeetCode OJ 之 Climbing Stairs ( 爬楼梯 )
- [leetcode]Climbing Stairs(爬楼梯 C语言)
- [LeetCode] Climbing stairs 爬楼梯问题
- LeetCode-70-Climbing Stairs(爬楼梯)
- 爬楼梯(LintCode Climbing Stairs)
- 70. Climbing Stairs (爬楼梯)
- LintCode Climbing Stairs 爬楼梯
- leetcode 70.Climbing Stairs-爬楼梯|动态规划
- Leetcode 70. Climbing Stairs 爬楼梯 解题报告
- LeetCode OJ-70. Climbing Stairs(爬楼梯问题)
- 计算机网络知识(路由器交换机的配置)
- Hadoop工作流引擎之Oozie3.3.2
- hdu 4638 Group (莫队算法 || 离线线段树)
- OC基础知识汇总
- 【C++】两个关于vector和iterator的练习
- LeetCode | Climbing Stairs(爬楼梯)
- linux系统学习之awk命令
- poj1019 Number Sequence
- Oozie配置说明
- 使用 JavaScript 拦截和跟踪浏览器中的 HTTP 请求
- XMPP协议的优点和缺点?
- 【编程珠玑——01】位向量
- 主键和外键
- wxPython学习笔记(四)