15 Jun 2024C++ / Python / TypeScriptEasy

Climbing Stairs

Collected C++, Python, TypeScript solutions for climbing stairs. Add a dedicated write-up later if you want deeper notes.

auto-generated entry for Climbing Stairs. the solution files are available below.

solution files

  • C++ climbing-stairs/synced-solution.cpp
  • Python climbing-stairs/synced-solution.py
  • TypeScript climbing-stairs/synced-solution.ts

Solution files

Pythonclimbing-stairs/synced-solution.py
class Solution:
    def climbStairs(self, n: int) -> int:
        a, b = class="syntax-number">1, class="syntax-number">1
        for _ in range(n-class="syntax-number">1):
            a, b = b, a+b
        return b
C++climbing-stairs/synced-solution.cpp
class Solution {
public:
    int climbStairs(int n) {
        int a = class="syntax-number">1, b = class="syntax-number">1;
        for (int i = class="syntax-number">1; i < n; i++) { int c = a+b; a = b; b = c; }
        return b;
    }
};
TypeScriptclimbing-stairs/synced-solution.ts
function climbStairs(n: number): number {
    let a = class="syntax-number">1, b = class="syntax-number">1;
    for (let i = class="syntax-number">1; i < n; i++) [a, b] = [b, a + b];
    return b;
}