[Leetcode]@python 70. Climbing Stairs
2016-01-13 10:34
281 查看
题目链接
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?
题目大意
爬楼梯,一次可以爬一步或者两步。如果要爬n层,问一共有多少种爬法。比如说,如果是3层,可以有[[1,1,1],[1,2],[2,1]]共3种方法。解题思路
可以使用DP进行求解,是斐波那契数列的变形,arr = arr[n-1] + arr[n-2] (n>2)
代码
class Solution(object): def climbStairs(self, n): """ :type n: int :rtype: int """ if n == 1 or n == 2: return n arr = [0 for i in range(n+1)] arr[1] = 1 arr[2] = 2 for i in range(3,n+1): arr[i] = arr[i-1]+arr[i-2] return arr
相关文章推荐
- 我的Python成长之路---第二天---Python基础(7)---2016年1月9日(晴)
- python教程
- Python语言基础
- [Leetcode]@python 69. Sqrt(x)
- python爬虫抢火车票
- Python + Requests + BeautifulSoup每日BUG汇总
- Robot Framework: 自定义自己的python库
- python赋值对象的指向改变规律
- WingIDE5.1安装以及激活(Python集成环境的搭建)
- [Leetcode]6. ZigZag Conversion @python
- Python时间模块datetime、time、calendar的使用方法
- 详解python时间模块中的datetime模块
- python基础入门学习笔记(Python环境搭建)
- Python数据类型学习笔记
- python编辑工具pycharm中文输入报错解决方法
- [Python]python中的__import__
- python学习第三天
- Python爬虫学习稿(三)
- python学习记录(1)
- Python合并两个列表的方法