-
Notifications
You must be signed in to change notification settings - Fork 452
Expand file tree
/
Copy pathSolution1.java
More file actions
36 lines (25 loc) · 783 Bytes
/
Solution1.java
File metadata and controls
36 lines (25 loc) · 783 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
// 递归求斐波那契数列
public class Solution1 {
private int num = 0;
public int fib( int n ){
num ++;
if( n == 0 )
return 0;
if( n == 1 )
return 1;
return fib(n-1) + fib(n-2);
}
public int getNum(){
return num;
}
public static void main(String[] args) {
int n = 42;
Solution1 solution = new Solution1();
long startTime = System.currentTimeMillis();
int res = solution.fib(n);
long endTime = System.currentTimeMillis();
System.out.println("fib(" + n + ") = " + res);
System.out.println("time : " + (endTime - startTime) + " ms");
System.out.println("run function fib() " + solution.getNum() + " times.");
}
}