`

Climbing Stairs——Dynamic Programming

 
阅读更多

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?

class Solution(object):
    def climbStairs(self, n):
        """
        :type n: int
        :rtype: int
        """
        w = [0]*n
        if n == 0:
        	return 0
        if n == 1:
        	return 1
        if n == 2:
        	return 2

        w[0] = 1
        w[1] = 2
        for i in range(2,n):
        	w[i] = w[i-1]+w[i-2]

        return w[n-1]

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics