本文主要是介绍leetcode#70. 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?
Note: Given n will be a positive integer.
很无聊的题,其实就是个斐波那契数列,虽然我没搞懂是怎么回事,但是试了前几个数试出来的,哈哈
代码
class Solution(object):def climbStairs(self, n):""":type n: int:rtype: int"""pre = 1after = 2i = 1while i < n:temp = prepre = afterafter += tempi += 1return pre
总结
很无聊的水题,不过在小嘎嘎那里学到了简洁的写法,可以避免temp的应用
r, a, b = 0, 0, 1
while r < n:a, b = b, a + br = r + 1
return r
这篇关于leetcode#70. Climbing Stairs的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!