File tree 1 file changed +23
-0
lines changed
1 file changed +23
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution (object ):
2
+ def climbStairs (self , n ):
3
+ """
4
+ Given a number n, return the number of distinct ways to climb to the top of a staircase with n steps.
5
+
6
+ :type n: int
7
+ :rtype: int
8
+ """
9
+ if n == 1 :
10
+ return 1
11
+ elif n == 2 :
12
+ return 2
1
13
14
+ prev1 = 1 # Base case: the number of ways to reach the first step
15
+ prev2 = 2 # Base case: the number of ways to reach the second step
16
+ current = 0
17
+
18
+ # Start from step 3 and calculate the number of ways to reach each subsequent step
19
+ for i in range (3 , n + 1 ):
20
+ current = prev1 + prev2 # The number of ways to reach the current step is the sum of the previous two
21
+ prev1 = prev2 # Update prev1 to be the previous step
22
+ prev2 = current # Update prev2 to be the current step
23
+
24
+ return current
You can’t perform that action at this time.
0 commit comments