70. Climbing Stairs
来源:互联网 发布:python 气泡提醒 编辑:程序博客网 时间:2024/06/06 07:20
原文题目:
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?
Note: Given n will be a positive integer.
读题:
这个题目一看就知道是斐波那契数列的变形,因此有两种方法解决:递归和非递归,这里都给出,由于使用递归时的处理时间太长,提交系统后超时,因此提交采用非递归。
非递归方法:
class Solution(object): def climbStairs(self, n): """ :type n: int :rtype: int """ if n < 0: return 0 if n == 1 or n ==0: return 1 result = 1 pre = 1 while n >= 2: n -= 1 nextvalue = pre pre = result result = pre + nextvalue return result递归方法:
class Solution(object): def climbStairs(self, n): """ :type n: int :rtype: int """ if n < 0: return 0 if n == 1 or n ==0: return 1 else: return self.climbStairs(n-1) + self.climbStairs(n-2)
阅读全文
0 0
- [LeetCode]70.Climbing Stairs
- 70.Climbing Stairs
- LeetCode --- 70. Climbing Stairs
- [Leetcode] 70. Climbing Stairs
- [leetcode] 70.Climbing Stairs
- 70.Climbing Stairs
- 70. Climbing Stairs
- 70.Climbing Stairs
- 70. Climbing Stairs
- [leetCode]70. Climbing Stairs
- 70. Climbing Stairs
- 70. Climbing Stairs LeetCode
- 70. Climbing Stairs
- 70. Climbing Stairs
- 70. Climbing Stairs
- [LeetCode]70. Climbing Stairs
- 【LeetCode】70. Climbing Stairs
- 70. Climbing Stairs
- 第一周-PDCA学习模型
- linux查看服务安装目录
- mysql主从复制
- angular2之打包
- MFC 的多视窗设计
- 70. Climbing Stairs
- asp.net 计算两个时间差
- 数组的32场演唱会
- DG正常启动后的主库与备库的状态
- java.lang.UnsatisfiedLinkError No implementation found for void com.baidu.speech.core.BDSSDKLoader.S
- Fragment生命周期
- vue.js技术知识点之-将json里面的字符串转为json对象并循环输出
- Java Integer和String内存存储
- Matlab保存图像的方法总结