《剑指offer》刷题笔记(递归和循环):跳台阶



题目描述:

一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法。

解题思路:

对于n级台阶,第一步有2种跳法:跳1级、跳2级;
跳1级,剩下n-1级,则剩下跳法是f(n-1)
跳2级,剩下n-2级,则剩下跳法是f(n-2)
所以f(n)=f(n-1)+f(n-2)。
其中:f(1) = 1, f(2) = 2。

实现f(n)=f(n-1)+f(n-2)的方法有很多种,递归、循环都可以。

注意:由于递归比较耗费时间,加上python的运行效率本来就低,所以python的递归调用一般在牛客网的实例测试中下总是超时。

C++版代码实现:

递归:

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
int jumpFloor(int number) {
if(number <= 0)
return 0;
else if(number < 3)
return number;
else
return jumpFloor(number-1)+jumpFloor(number-2);
}
};

循环:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
int jumpFloor(int number) {
if(number <= 0)
return 0;
else if(number < 3)
return number;
int first = 1, second = 2, third = 0;
for (int i = 3; i <= number; i++) {
third = first + second;
first = second;
second = third;
}
return third;
}
};

Python 代码实现:

递归:

1
2
3
4
5
6
7
8
9
10
# -*- coding:utf-8 -*-
class Solution:
def jumpFloor(self, number):
# write code here
if number <= 0:
return 0
elif number < 3:
return number
else:
return self.jumpFloor(number-1) + self.jumpFloor(number-2)

循环:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# -*- coding:utf-8 -*-
class Solution:
def jumpFloor(self, number):
# write code here
if number <= 0:
return 0
elif number < 3:
return number
first = 1
second = 2
third = 0
for i in range(3,number+1):
third = first + second
first = second
second = third
return third

系列教程持续发布中,欢迎订阅、关注、收藏、评论、点赞哦~~( ̄▽ ̄~)~

完的汪(∪。∪)。。。zzz

0%