-
Notifications
You must be signed in to change notification settings - Fork 0
/
509.斐波那契数.rs
66 lines (65 loc) · 1.12 KB
/
509.斐波那契数.rs
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* @lc app=leetcode.cn id=509 lang=rust
*
* [509] 斐波那契数
*
* https://leetcode-cn.com/problems/fibonacci-number/description/
*
* algorithms
* Easy (65.25%)
* Likes: 70
* Dislikes: 0
* Total Accepted: 27.9K
* Total Submissions: 42.8K
* Testcase Example: '2'
*
* 斐波那契数,通常用 F(n) 表示,形成的序列称为斐波那契数列。该数列由 0 和 1 开始,后面的每一项数字都是前面两项数字的和。也就是:
*
* F(0) = 0, F(1) = 1
* F(N) = F(N - 1) + F(N - 2), 其中 N > 1.
*
*
* 给定 N,计算 F(N)。
*
*
*
* 示例 1:
*
* 输入:2
* 输出:1
* 解释:F(2) = F(1) + F(0) = 1 + 0 = 1.
*
*
* 示例 2:
*
* 输入:3
* 输出:2
* 解释:F(3) = F(2) + F(1) = 1 + 1 = 2.
*
*
* 示例 3:
*
* 输入:4
* 输出:3
* 解释:F(4) = F(3) + F(2) = 2 + 1 = 3.
*
*
*
*
* 提示:
*
*
* 0 ≤ N ≤ 30
*
*
*/
// @lc code=start
impl Solution {
pub fn fib(n: i32) -> i32 {
if n == 1 || n == 0 {
return n;
}
return Self::fib(n - 1) + Self::fib(n - 2);
}
}
// @lc code=end