-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path70_Climbing_Stairs.py
More file actions
52 lines (42 loc) · 966 Bytes
/
70_Climbing_Stairs.py
File metadata and controls
52 lines (42 loc) · 966 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Method 1
class Solution(object):
def climbStairs(self, n):
"""
:type n: int
:rtype: int
"""
fib = lambda n:1 if n<=2 else fib(n-1)+fib(n-2)
return fib(n+1)
# Method 2
class Solution(object):
def climbStairs(self, n):
"""
:type n: int
:rtype: int
"""
from math import sqrt
return int(sqrt(5)/5*((((1+sqrt(5))/2))**(n+1)-(((1-sqrt(5))/2))**(n+1)))
# Method 3
class Solution(object):
def climbStairs(self, n):
"""
:type n: int
:rtype: int
"""
a=1
b=1
def fib(n, a, b):
while n != 0:
a, b = b, a+b
n -= 1
return b
return fib(n-1, a, b)
# method 4
class Solution:
def climbStairs(self, n: int) -> int:
p, q, r = 0, 0, 1
for i in range(n):
p = q
q = r
r = p+q
return r