Skip to content

Commit a5cc845

Browse files
committed
Fibonacci Number
1 parent ab7b041 commit a5cc845

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

509-fibonacci-number.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
Problem Link: https://leetcode.com/problems/fibonacci-number/
3+
4+
The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is
5+
the sum of the two preceding ones, starting from 0 and 1. That is,
6+
F(0) = 0, F(1) = 1
7+
F(N) = F(N - 1) + F(N - 2), for N > 1.
8+
Given N, calculate F(N).
9+
10+
Example 1:
11+
Input: 2
12+
Output: 1
13+
Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1.
14+
15+
Example 2:
16+
Input: 3
17+
Output: 2
18+
Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2.
19+
20+
Example 3:
21+
Input: 4
22+
Output: 3
23+
Explanation: F(4) = F(3) + F(2) = 2 + 1 = 3.
24+
25+
Note:
26+
0 ≤ N ≤ 30.
27+
"""
28+
class Solution:
29+
def fib(self, N):
30+
"""
31+
:type N: int
32+
:rtype: int
33+
"""
34+
if N == 0:
35+
return 0
36+
l = [0,1]
37+
for i in range(2,N):
38+
s = l[i-1] + l[i-2]
39+
l.append(s)
40+
return l[N-1] + l[N-2]

0 commit comments

Comments
 (0)