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]

 

猜你喜欢

转载自qu66q.iteye.com/blog/2317231
今日推荐