Skip to content

Commit a3aa429

Browse files
committed
Improve performance
1 parent 6235617 commit a3aa429

File tree

1 file changed

+4
-10
lines changed

1 file changed

+4
-10
lines changed

solution/0509.Fibonacci/Solution.cpp

+4-10
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
class Solution {
22
public:
33
int fib(int N) {
4-
if (N < 2)
5-
return N > 0? 1: 0 ;
6-
int aN_2 = 0, aN_1 = 1, aN ;
7-
while (--N)
8-
{
9-
aN = aN_2 + aN_1 ;
10-
aN_2 = aN_1 ;
11-
aN_1 = aN ;
12-
}
13-
return aN ;
4+
int a[2] = {0, 1} ;
5+
for (int i = 2; i <= N; ++i)
6+
a[i&1] += a[i&1^1] ;
7+
return a[N&1] ;
148
}
159
};

0 commit comments

Comments
 (0)