FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
LeetCode/src/main/java/L0509_FibonacciNumber.java at master · LjyYano/LeetCode · GitHub
LjyYano
/
LeetCode
Public
Notifications
You must be signed in to change notification settings
Fork
125
Star
342
Code
Issues
0
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
LeetCode
/
src
/
main
/
java
/
L0509_FibonacciNumber.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
68 lines (59 loc) · 1.64 KB
Breadcrumbs
LeetCode
/
src
/
main
/
java
/
L0509_FibonacciNumber.java
Copy path
File metadata and controls
68 lines (59 loc) · 1.64 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
67
68
/**
* https://leetcode.cn/problems/fibonacci-number/
*
* 斐波那契数 (通常用 F(n) 表示)形成的序列称为 斐波那契数列 。
* 该数列由 0 和 1 开始,后面的每一项数字都是前面两项数字的和。也就是:
*
* F(0) = 0,F(1) = 1
* F(n) = F(n - 1) + F(n - 2),其中 n > 1
*
* 给定 n ,请计算 F(n) 。
*
* 示例 1:
* 输入:n = 2
* 输出:1
* 解释:F(2) = F(1) + F(0) = 1 + 0 = 1
*
* 示例 2:
* 输入:n = 3
* 输出:2
* 解释:F(3) = F(2) + F(1) = 1 + 1 = 2
*
* 示例 3:
* 输入:n = 4
* 输出:3
* 解释:F(4) = F(3) + F(2) = 2 + 1 = 3
*
* 提示:
* - 0 <= n <= 30
*/
public
class
L0509_FibonacciNumber
{
/**
* 动态规划 - 空间优化版本
* 只需要保存前两个数
*/
public
int
fib
(
int
n
) {
if
(
n
<=
1
) {
return
n
;
}
int
prev2
=
0
;
// F(0)
int
prev1
=
1
;
// F(1)
for
(
int
i
=
2
;
i
<=
n
;
i
++) {
int
curr
=
prev1
+
prev2
;
prev2
=
prev1
;
prev1
=
curr
;
}
return
prev1
;
}
public
static
void
main
(
String
[]
args
) {
L0509_FibonacciNumber
solution
=
new
L0509_FibonacciNumber
();
// 测试用例 1
System
.
out
.
println
(
solution
.
fib
(
2
));
// 预期输出:1
// 测试用例 2
System
.
out
.
println
(
solution
.
fib
(
3
));
// 预期输出:2
// 测试用例 3
System
.
out
.
println
(
solution
.
fib
(
4
));
// 预期输出:3
// 测试用例 4
System
.
out
.
println
(
solution
.
fib
(
10
));
// 预期输出:55
}
}
Back
|
FazBrowse Home
|
New Git URL