The Climbing Stairs solutions are below.
15 Jun 2024•Easy•C++ / Python / TypeScript
Climbing Stairs
C++, Python and TypeScript solutions for Climbing Stairs.
Solution files
Python•climbing-stairs/synced-solution.py
class Solution:
def climbStairs(self, n: int) -> int:
a, b = 1, 1
for _ in range(n-1):
a, b = b, a+b
return b
C++•climbing-stairs/synced-solution.cpp
class Solution {
public:
int climbStairs(int n) {
int a = 1, b = 1;
for (int i = 1; i < n; i++) { int c = a+b; a = b; b = c; }
return b;
}
};
TypeScript•climbing-stairs/synced-solution.ts
function climbStairs(n: number): number {
let a = 1, b = 1;
for (let i = 1; i < n; i++) [a, b] = [b, a + b];
return b;
}