leetcode#70. Climbing Stairs

来源:互联网 发布:nginx 多个二级域名 编辑:程序博客网 时间:2024/06/01 07:26

题目

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 = 1        after = 2        i = 1        while i < n:            temp = pre            pre = after            after += temp            i += 1        return pre

总结

很无聊的水题,不过在小嘎嘎那里学到了简洁的写法,可以避免temp的应用

r, a, b = 0, 0, 1while r < n:    a, b = b, a + b    r = r + 1return r
原创粉丝点击