Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

70. Climbing Stairs #211

Open
Tcdian opened this issue Jun 13, 2020 · 1 comment
Open

70. Climbing Stairs #211

Tcdian opened this issue Jun 13, 2020 · 1 comment

Comments

@Tcdian
Copy link
Owner

Tcdian commented Jun 13, 2020

70. Climbing Stairs

假设你正在爬楼梯。需要 n 阶你才能到达楼顶。

每次你可以爬 12 个台阶。你有多少种不同的方法可以爬到楼顶呢?

Example 1

Input: 2
Output: 2
Explanation: There are two ways to climb to the top.
1. 1 step + 1 step
2. 2 steps

Example 2

Input: 3
Output: 3
Explanation: There are three ways to climb to the top.
1. 1 step + 1 step + 1 step
2. 1 step + 2 steps
3. 2 steps + 1 step

Note

  • n 是一个正整数
@Tcdian
Copy link
Owner Author

Tcdian commented Jun 13, 2020

Solution

  • JavaScript Solution
/**
 * @param {number} n
 * @return {number}
 */
var climbStairs = function(n) {
    const dp = new Array(n + 1);
    for (let i = 0; i <= n; i++) {
        if (i < 2) {
            dp[i] = 1;
        } else {
            dp[i] = dp[i - 1] + dp[i - 2];
        }
    }
    return dp[n];
};
  • TypeScript Solution
function climbStairs(n: number): number {
    const dp: number[] = new Array(n + 1);
    for (let i = 0; i <= n; i++) {
        if (i < 2) {
            dp[i] = 1;
        } else {
            dp[i] = dp[i - 1] + dp[i - 2];
        }
    }
    return dp[n];
}

@Tcdian Tcdian removed the Classic label Jul 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant