Skip to content

Commit 8b61970

Browse files
committedJan 8, 2023
Add solution #509
1 parent be1db37 commit 8b61970

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@
144144
476|[Number Complement](./0476-number-complement.js)|Easy|
145145
500|[Keyboard Row](./0500-keyboard-row.js)|Easy|
146146
506|[Relative Ranks](./0506-relative-ranks.js)|Easy|
147+
509|[Fibonacci Number](./0509-fibonacci-number.js)|Easy|
147148
520|[Detect Capital](./0520-detect-capital.js)|Easy|
148149
541|[Reverse String II](./0541-reverse-string-ii.js)|Easy|
149150
542|[01 Matrix](./0542-01-matrix.js)|Medium|

‎solutions/0509-fibonacci-number.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* 509. Fibonacci Number
3+
* https://leetcode.com/problems/fibonacci-number/
4+
* Difficulty: Easy
5+
*
6+
* The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence,
7+
* such that each number is the sum of the two preceding ones, starting from 0 and 1. That is:
8+
* - F(0) = 0, F(1) = 1
9+
* - F(n) = F(n - 1) + F(n - 2), for n > 1.
10+
* Given n, calculate F(n).
11+
*/
12+
13+
/**
14+
* @param {number} n
15+
* @return {number}
16+
*/
17+
var fib = function(n) {
18+
const nums = [0, 1];
19+
20+
for (let i = 2; i <= n; i++) {
21+
nums.push(nums[i - 2] + nums[i - 1]);
22+
}
23+
24+
return nums[n];
25+
};

0 commit comments

Comments
 (0)
Please sign in to comment.