您的位置:首页 > 编程语言 > Python开发

[LeetCode]题解(python):070-Climbing Stairs

2015-12-14 16:02 561 查看
[b]题目来源:[/b]

  https://leetcode.com/problems/climbing-stairs/

[b]题意分析:[/b]

  爬楼梯,一次可以爬一步或者两步。如果要爬n层,问一共有多少种爬法。比如说,如果是3层,可以有[[1,1,1],[1,2],[2,1]]共3种方法。

[b]题目思路:[/b]

  这是一个典型的动态规划问题。n层的方法数 = n - 1层的方法数 + n - 2层的方法数。

[b]代码(Python):[/b]

  

class Solution(object):
def climbStairs(self, n):
"""
:type n: int
:rtype: int
"""
if n == 1 or n == 0:
return 1
i,tmp1,tmp2 = 2,1,1
while i <= n:
tmp1 = tmp1 + tmp2
if i == n:
return tmp1
i += 1
tmp2 = tmp1 + tmp2
if i == n:
return tmp2
i += 1


View Code

转载请注明出处:http://www.cnblogs.com/chruny/p/5045540.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: