70. Climbing Stairs
題目原文
You are climbing a stair case. It takes n steps to reach to the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
Note:
Given n will be a positive integer.
Example 1:
Example 2:
解題思路
n = 1,output = 1
n = 2,output = 2
n = 3,output = 3
n = 4,output = 5
n = 5,output = 8
看似很像是費式數列,但注意的是遞迴版本會超過時間。
程式解答
Recursive: Time Limit Exceeded
Iterative: Accepted
Last updated
Was this helpful?