# 01-爬楼梯问题「递归/Python」

1. 1 步 + 1 步
2. 2 步

## # 示例2

1. 1 步 + 1 步 + 1 步
2. 1 步 + 2 步
3. 2 步 + 1 步

``````        if n == 1:
return 1
elif n == 2:
return 2
else:
s1 = self.climbStairs(n-1)
s2 = self.climbStairs(n-2)
return s1+s2
``````

``````        nums = [0,1,2]
if n == 1:
return nums[1]
elif n == 2:
return nums[2]
else:
for i in range(3,n+1):
nums.append(nums[i-1] + nums[i-2])
return nums[n]
``````

``````        condition = [0] * (n + 1)
condition[0] = 1
condition[1] = 1
for i in range(2, n+1):
condition[i] = condition[i-1] + condition[i-2]
return condition[n]
``````

Loops may achieve a performance gain for your program. Recursion may achieve a performance gain for your programmer. Choose which is more important in your situation!

AI悦创·编程一对一

AI悦创·推出辅导班啦，包括「Python 语言辅导班、C++ 辅导班、java 辅导班、算法/数据结构辅导班、少儿编程、pygame 游戏开发」，全部都是一对一教学：一对一辅导 + 一对一答疑 + 布置作业 + 项目实践等。当然，还有线下线上摄影课程、Photoshop、Premiere 一对一教学、QQ、微信在线，随时响应！微信：Jiabcdefh

C++ 信息奥赛题解，长期更新！长期招收一对一中小学信息奥赛集训，莆田、厦门地区有机会线下上门，其他地区线上。微信：Jiabcdefh

• 0
• 0
• 0
• 0
• 0
• 0

• 按正序
• 按倒序
• 按热度