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:
Input: 2
Output: 2
Explanation: There are two ways to climb to the top.
1. 1 step + 1 step
2. 2 steps
Example 2:
Input: 3
Output: 3
Explanation: There are three ways to climb to the top.
1. 1 step + 1 step + 1 step
2. 1 step + 2 steps
3. 2 steps + 1 step
复制代码
你正在爬楼梯。到达山顶须要n步。 每次你能够爬1或2级台阶。你能够用几种不一样的方式爬到山顶? 注意:给定n是一个正整数。 示例1: 输入:2 输出:2 说明:爬到山顶有两种方法。bash
- 1步+ 1步
- 2步 示例2: 输入:3 输出:3 说明:爬到山顶有三种方法。
- 1步+ 1步+ 1步
- 1步+ 2步
- 2步+ 1步
本题要求很明确,就是根据目标阶梯,预测一下须要多少中1,2组合方式走完整个阶梯,粗看貌似很麻烦,用循环来弄不知道有多少状况,各类if,可是换个角度来看,若是咱们倒着推理,好比最后一级只多是1步上来或者是2步上来,这样子逆退,就找出来一种递归的方式,可是这种方法比较耗时间,没有经过,不知道对不对,因此换一种方式,咱们试试正向思惟,3级是2步和一步跨上去的,那是否是2+1?一样4级,也是由两步和一步跨上去的,也就是n(4)=n(2)+n(3),哎呦,好像斐波那契数列哦。相似的推理,其实咱们能够猜想出规律n(n)=n(n-1)+n(n-1).因此本题就解决了ui
按照咱们的思路来编辑,代码以下spa
if (n < 0) {
return 0;
}
if (n <= 2) {
return n;
}
int a = 1;
int b = 2;
int result = 0;
for (int i = 3; i <= n; i++) {
result = a + b;
a = b;
b = result;
}
return result;
复制代码
时间复杂度: 该方案用了循环m因此f(n)=(n)=n;因此O(f(n))=O(n),即T(n)=O(n)翻译
空间复杂度: 该方案使用了没有使用额外空间,因此空间复杂度是O(n)=O(1);code
本题的大体解法如上所诉, 能够经过逆推来发现特定的规律,直接想多是个很大的问题,因此能够考虑换个思惟。递归