Created
October 13, 2020 05:28
-
-
Save aershov24/55d5f4cb3b9025488fcff37eacb2bd16 to your computer and use it in GitHub Desktop.
Markdium-14 Fibonacci Interview Questions (SOLVED) To Brush Before Coding Interview
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private static final int FIB_0 = 0; | |
| private static final int FIB_1 = 1; | |
| private int calcFibonacci(final int target) { | |
| if (target == 0) { return FIB_0; } | |
| if (target == 1) { return FIB_1; } | |
| return calcFibonacci(target, 1, FIB_1, FIB_0); | |
| } | |
| private int calcFibonacci(final int target, final int previous, final int fibPrevious, final int fibPreviousMinusOne) { | |
| final int current = previous + 1; | |
| final int fibCurrent = fibPrevious + fibPreviousMinusOne; | |
| // If you want, print here / memoize for future calls | |
| if (target == current) { return fibCurrent; } | |
| return calcFibonacci(target, current, fibCurrent, fibPrevious); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment