{ "type": "code_options", "author": "https://github.com/qiyuangong/leetcode", "source": "solution.md", "exercise_id": "0a12ae6577704d18b37db6956fe16aa8", "keywords": "记忆化搜索,数学,动态规划", "title": "爬楼梯", "desc": [ { "content": "\n
假设你正在爬楼梯。需要 n 阶你才能到达楼顶。
每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?
注意:给定 n 是一个正整数。
示例 1:
输入: 2
输出: 2
解释: 有两种方法可以爬到楼顶。1. 1 阶 + 1 阶2. 2 阶
示例 2:
输入: 3", "language": "markdown" } ], "answer": [ { "content": "", "language": "python" } ], "prepared": [ [ { "content": "", "language": "python" } ], [ { "content": "", "language": "python" } ], [ { "content": "", "language": "python" } ] ], "template": { "content": "class Solution(object):\n\tdef climbStairs(self, n):\n\t\tif n <= 1:\n\t\t\treturn 1\n\t\tdp = [1] * 2\n\t\tfor i in range(2, n + 1):\n\t\t\tdp[1], dp[0] = dp[1] + dp[0], dp[1]\n\t\treturn dp[1]\n# %%\ns = Solution()\nprint(s.climbStairs(2))", "language": "python" }, "node_id": "dailycode-40b9002d2beb42aeabf9947bc8af5932", "license": "csdn.net", "created_at": 1637894161, "topic_link": "https://bbs.csdn.net/topics/600470125" }
输出: 3
解释: 有三种方法可以爬到楼顶。1. 1 阶 + 1 阶 + 1 阶2. 1 阶 + 2 阶3. 2 阶 + 1 阶