Leetcode 70. Climbing Stairs
来源:互联网 发布:广东省网络作家协会 编辑:程序博客网 时间:2024/05/20 20:58
题目链接:https://leetcode.com/problems/climbing-stairs/
题目描述:
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?
Subscribe to see which companies asked this question
两种方法,循环和递归,递归超时(功能正确)
代码1:循环
class Solution {public: int climbStairs(int n) { if(n==1||n==2) return n; int fn_1=2; int fn_2=1; int tmp; for(int i=3;i<=n;i++) { tmp=fn_1+fn_2; fn_2=fn_1; fn_1=tmp; } return tmp; }};
代码2:递归
class Solution {public: int climbStairs(int n) { return climb(n); } int climb(int n) { int re; if(n==1||n==2) return n; re=climb(n-1)+climb(n-2); return re; }};
0 0
- [LeetCode]70.Climbing Stairs
- LeetCode --- 70. Climbing Stairs
- [Leetcode] 70. Climbing Stairs
- [leetcode] 70.Climbing Stairs
- [leetCode]70. Climbing Stairs
- 70. Climbing Stairs LeetCode
- [LeetCode]70. Climbing Stairs
- 【LeetCode】70. Climbing Stairs
- leetcode 70. Climbing Stairs
- leetcode 70. Climbing Stairs
- LeetCode *** 70. Climbing Stairs
- 【LeetCode】70. Climbing Stairs
- LeetCode 70. Climbing Stairs
- leetcode 70. Climbing Stairs
- [LeetCode]70. Climbing Stairs
- 【LeetCode】70. Climbing Stairs
- LeetCode 70. Climbing Stairs
- #leetcode#70.Climbing Stairs
- 虚幻4 C++ UFUNCTION 宏 参数解析
- gcc 编译出现undefined reference to‘pthread_create’的错误
- 编写高质量代码之读书笔记2
- redis五种运用类型场景
- cortex-m3的中断处理
- Leetcode 70. Climbing Stairs
- Message Flood sdutoj 1500 (STL map 的使用)
- javascript-python-git学习
- 编写高质量代码之读书笔记3
- java基础总结3
- 欢迎使用CSDN-markdown编辑器
- 您不得不知道的一件大事——Internet of things
- 基础类库:包装类
- 292_ActionBar的使用