Skip to content

Instantly share code, notes, and snippets.

Created July 28, 2013 07:19
Show Gist options
  • Select an option

  • Save anonymous/d23efa6ad0c662c9d261 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/d23efa6ad0c662c9d261 to your computer and use it in GitHub Desktop.
private static final Map<Integer, Long> fibMap = new HashMap<Integer, Long>();
//Is storing the result dynamic programming?
private static long fibonacci(int n, boolean useMap) {
if (n <= 0)
return 1;
else if (n == 1)
return 2;
else if (useMap) {
if (fibMap.containsKey(n))
return fibMap.get(n);
else {
fibMap.put(n, fibonacci(n - 1, useMap) + fibonacci(n - 2, useMap));
return fibMap.get(n);
}
} else
return fibonacci(n - 1, useMap) + fibonacci(n - 2, useMap);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment