-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode509.java
More file actions
37 lines (33 loc) · 806 Bytes
/
LeetCode509.java
File metadata and controls
37 lines (33 loc) · 806 Bytes
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
public class LeetCode509 {
public static void main(String[] args) {
// 输入:n = 2
// 输出:1
System.out.println(new Solution509().fib(2));
// 输入:n = 3
// 输出:2
System.out.println(new Solution509().fib(3));
// 输入:n = 4
// 输出:3
System.out.println(new Solution509().fib(4));
// 输入:n = 5
// 输出:5
System.out.println(new Solution509().fib(5));
}
}
class Solution509 {
public int fib(int n) {
if (n == 0) {
return 0;
}
if (n == 1) {
return 1;
}
int a = 0;
int b = 1;
for (int i = 2; i <= n; i++) {
b = a + b;
a = b - a;
}
return b;
}
}