-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07.h
More file actions
35 lines (29 loc) · 664 Bytes
/
07.h
File metadata and controls
35 lines (29 loc) · 664 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
/*
* @Author: FreedomLy
* @Date: 2018-04-24 10:33:44
* @Last Modified by: FreedomLy
* @Last Modified time: 2018-04-24 10:38:15
* 题目
* 写一个函数,输入 n,求斐波那契(Fibonacci)数列的第n项
*/
#pragma once
#include <vector>
using std::vector;
class Solution07 {
public:
long long fibonacci(unsigned n)
{
vector<int> res{0, 1};
if (n < 2)
return res[n];
long long f1 = res[0], f2 = res[1];
long long fib = 0;
for (unsigned i = 2; i <= n; ++i)
{
fib = f1 + f2;
f1 = f2;
f2 = fib;
}
return fib;
}
};