We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
Difficulty: 简单
Related Topics: 记忆化搜索, 数学, 动态规划
假设你正在爬楼梯。需要 n 阶你才能到达楼顶。
n
每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?
1
2
示例 1:
输入:n = 2 输出:2 解释:有两种方法可以爬到楼顶。 1\. 1 阶 + 1 阶 2\. 2 阶
示例 2:
输入:n = 3 输出:3 解释:有三种方法可以爬到楼顶。 1\. 1 阶 + 1 阶 + 1 阶 2\. 1 阶 + 2 阶 3\. 2 阶 + 1 阶
提示:
1 <= n <= 45
Language: JavaScript
/** * @param {number} n * @return {number} * 题目分析: * 第1级台阶:1种方法(爬1级) * 第2级台阶:2种方法(爬1级或爬2级) * 第n级台阶:dp[i] = dp[i-1] + dp[i-2] */ // 动态规划 // 滚动数组思想 // var climbStairs = function(n) { // if (n === 1) return 1 // if (n === 2) return 2 // let [pre, cur, sum] = [1, 1, 2] // for (let i = 3; i <= n; i++) { // [pre, cur] = [cur, sum] // sum = pre + cur // } // return sum // } var climbStairs = function(n) { const dp = [] dp[0] = 1 dp[1] = 1 for (let i = 2; i <= n; i++) { dp[i] = dp[i-1] + dp[i-2] } return dp[n] }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
70. 爬楼梯
Description
Difficulty: 简单
Related Topics: 记忆化搜索, 数学, 动态规划
假设你正在爬楼梯。需要
n
阶你才能到达楼顶。每次你可以爬
1
或2
个台阶。你有多少种不同的方法可以爬到楼顶呢?示例 1:
示例 2:
提示:
1 <= n <= 45
Solution
Language: JavaScript
The text was updated successfully, but these errors were encountered: