70. Climbing Stairs*
https://leetcode.com/problems/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:
C++ 实现 1
使用迭代的方法. 假设爬 n
楼的方法有 Tn
种, 那么有可能是爬到 n - 1
楼之后, 再走 1 步到达 n
楼, 有 Tn-1
种方法; 或者是爬到 n-2
楼之后, 再走 2 步达到 n
楼, 这有 Tn-2
种方法; 因此达到 n
楼的方法有:
类似于 1137. N-th Tribonacci Number* 这题.
代码如下:
C++ 实现 2
更简洁的写法: