Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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‎

Lines changed: 4 additions & 10 deletions
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)

AltStyle によって変換されたページ (->オリジナル) /