-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path0366. Fibonacci
More file actions
39 lines (35 loc) · 804 Bytes
/
0366. Fibonacci
File metadata and controls
39 lines (35 loc) · 804 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
38
39
public class Solution {
/**
* @param n: an integer
* @return: an ineger f(n)
*/
public int fibonacci(int n) {
if(n==2 || n==3){
return 1;
}
else if(n<=1){
return 0;
}
else{
return fibonacci(n-1)+fibonacci(n-2);
}
}
}
Time Limit Exceeded
90% test cases passed
public class Solution {
/**
* @param n: an integer
* @return: an ineger f(n)
*/
public int fibonacci(int n) {
ArrayList<Integer> arr = new ArrayList<>();
arr.add(0); arr.add(1);
for(int i=2; i<n; i++){
arr.add(arr.get(i-1)+arr.get(i-2));
}
return arr.get(n-1);
}
}
100% test cases passedTotal runtime 1522 ms
Your submission beats 71.00% Submissions!