-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathJumpFloor.cs
More file actions
52 lines (44 loc) · 1.42 KB
/
Copy pathJumpFloor.cs
File metadata and controls
52 lines (44 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
题目名称:
跳台阶
题目描述:
一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果)。
代码结构:
class Solution
{
public int JumpFloor(int number)
{
// write code here
}
}
*/
using System;
namespace JumpFloor {
class Solution {
/// <summary>
/// 解法
/// 基本思路:
/// 对于n级台阶,设一共有F(n)种跳法
/// 第一次青蛙可以选择跳1级,则剩下的跳法就是F(n-1)
/// 青蛙也可以选择跳2级,则剩下的跳法就是F(n-2)
/// 即F(n) = F(n-1) + F(n-2),这不就是斐波那契数列嘛
/// 所以求斐波那契数列的解法都可以用于这道题 详情可参考 Fibonacci.cs 文件
/// 这里给出简单的递归解法,其余斐波那契数列解法不再赘述
/// </summary>
public int JumpFloor(int number)
{
if(number <= 2){
return number;
}
return JumpFloor(number - 1) + JumpFloor(number - 2);
}
public void Test() {
int number = 0;
number = 2;
number = 3;
number = 4;
number = 39;
Console.WriteLine(JumpFloor(number));
}
}
}