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 eae73b8

Browse files
committed
Add: Fibonacci Number
1 parent 73c0236 commit eae73b8

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

‎problems/fibonacci-number/README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<!--|This file generated by command(leetcode description); DO NOT EDIT. |-->
2+
<!--+----------------------------------------------------------------------+-->
3+
<!--|@author Openset <openset.wang@gmail.com> |-->
4+
<!--|@link https://github.com/openset |-->
5+
<!--|@home https://github.com/openset/leetcode |-->
6+
<!--+----------------------------------------------------------------------+-->
7+
8+
## 509. Fibonacci Number (Easy)
9+
10+
<p>The&nbsp;<b>Fibonacci numbers</b>, commonly denoted&nbsp;<code>F(n)</code>&nbsp;form a sequence, called the&nbsp;<b>Fibonacci sequence</b>, such that each number is the sum of the two preceding ones, starting from <code>0</code> and <code>1</code>. That is,</p>
11+
12+
<pre>
13+
F(0) = 0,&nbsp; &nbsp;F(1)&nbsp;= 1
14+
F(N) = F(N - 1) + F(N - 2), for N &gt; 1.
15+
</pre>
16+
17+
<p>Given <code>N</code>, calculate <code>F(N)</code>.</p>
18+
19+
<p>&nbsp;</p>
20+
21+
<p><strong>Example 1:</strong></p>
22+
23+
<pre>
24+
<strong>Input:</strong> 2
25+
<strong>Output:</strong> 1
26+
<strong>Explanation:</strong> F(2) = F(1) + F(0) = 1 + 0 = 1.
27+
</pre>
28+
29+
<p><strong>Example 2:</strong></p>
30+
31+
<pre>
32+
<strong>Input:</strong> 3
33+
<strong>Output:</strong> 2
34+
<strong>Explanation:</strong> F(3) = F(2) + F(1) = 1 + 1 = 2.
35+
</pre>
36+
37+
<p><strong>Example 3:</strong></p>
38+
39+
<pre>
40+
<strong>Input:</strong> 4
41+
<strong>Output:</strong> 3
42+
<strong>Explanation:</strong> F(4) = F(3) + F(2) = 2 + 1 = 3.
43+
</pre>
44+
45+
<p>&nbsp;</p>
46+
47+
<p><strong>Note:</strong></p>
48+
49+
<p>0 &le; <code>N</code> &le; 30.</p>
50+
51+
52+
### Related Topics
53+
[[Array](https://github.com/openset/leetcode/tree/master/tag/array/README.md)]
54+
55+
### Similar Questions
56+
1. [Climbing Stairs](https://github.com/openset/leetcode/tree/master/problems/climbing-stairs) (Easy)
57+
1. [Split Array into Fibonacci Sequence](https://github.com/openset/leetcode/tree/master/problems/split-array-into-fibonacci-sequence) (Medium)
58+
1. [Length of Longest Fibonacci Subsequence](https://github.com/openset/leetcode/tree/master/problems/length-of-longest-fibonacci-subsequence) (Medium)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package fibonacci_number
2+
3+
func fib(N int) int {
4+
a, b := 0, 1
5+
for i := 0; i < N; i++ {
6+
a, b = b, a+b
7+
}
8+
return a
9+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package fibonacci_number
2+
3+
import "testing"
4+
5+
type caseType struct {
6+
input int
7+
expected int
8+
}
9+
10+
func TestFib(t *testing.T) {
11+
tests := [...]caseType{
12+
{
13+
input: 2,
14+
expected: 1,
15+
},
16+
{
17+
input: 3,
18+
expected: 2,
19+
},
20+
{
21+
input: 4,
22+
expected: 3,
23+
},
24+
{
25+
input: 1,
26+
expected: 1,
27+
},
28+
}
29+
for _, tc := range tests {
30+
output := fib(tc.input)
31+
if output != tc.expected {
32+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
33+
}
34+
}
35+
}

0 commit comments

Comments
(0)

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