#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
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?
'''
class Solution(object):
def climbStairs(self, n):
"""
:type n: int
:rtype: int
"""
if n < 1:
return 0
if n < 3:
return n
t1,t2 = 1,2
while n > 2:
n -= 1
tmp = t1 + t2
t1,t2 = t2,tmp
return t2
if __name__ == "__main__":
s = Solution()
print s.climbStairs(3)
print s.climbStairs(4)
65 leetcode - Climbing Stairs
最新推荐文章于 2024-09-14 18:52:56 发布