Skip to content

Commit 385e40b

Browse files
committed
跳台阶
1 parent 8283ba2 commit 385e40b

File tree

4 files changed

+69
-0
lines changed

4 files changed

+69
-0
lines changed

Code/7.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# -*- coding:utf-8 -*-
2+
class Solution:
3+
def jumpFloor(self, number):
4+
a=1
5+
b=2
6+
7+
# write code here
8+
if number == 1:
9+
return 1
10+
elif number == 2:
11+
return 2
12+
else:
13+
number=number-2
14+
while number != 0:
15+
c=a+b
16+
a=b
17+
b=c
18+
number -= 1
19+
20+
return b
21+
22+
23+
solution = Solution()
24+
print solution.jumpFloor(4)

Doc/文档模板.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# 题目
2+
3+
<center>知识点:xx</center>
4+
5+
## 题目描述
6+
7+
8+
9+
## 解题思路
10+
11+
### 思路一
12+
13+
14+
15+
### 思路二
16+
17+
## 代码
18+
19+
[这里](../Code/1.py)

Doc/跳台阶.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# 跳台阶
2+
3+
4+
## 题目描述
5+
6+
一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果)。
7+
8+
## 解题思路
9+
设共有m种不同跳法
10+
11+
- 如果n=1,那么m=1
12+
- 如果n=2,那么m=2
13+
14+
对于n阶台阶:
15+
16+
- case1:第一次跳了1个台阶,就有f(n-1)种跳法
17+
- case2:第一次挑个2个台阶,就有f(n-2)种跳法
18+
19+
所以总的n阶台阶的跳法f(n)=case1+case2=f(n-1)+f(n-2)
20+
21+
这是典型的斐波那契数列,需要注意的是尽量不要用递归去求解斐波那契数列,尽量使用循环,如果只需要第n项,不用保存所有n项数据,只需每次保存前2项即可。
22+
23+
## 代码
24+
25+
[这里](../Code/7.py)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
- [重建二叉树](./Doc/重建二叉树.md)
77
- [用两个栈实现队列](./Doc/用两个栈实现队列.md)
88
- [旋转数组的最小数字](./Doc/旋转数组的最小数字.md)
9+
- [跳台阶](./Doc/跳台阶.md)

0 commit comments

Comments
 (0)