Created
January 2, 2015 15:50
-
-
Save AlexAbraham1/304ea71ba7e3270df780 to your computer and use it in GitHub Desktop.
This file contains 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
public class FibStack { | |
public static void main(String[] args) { | |
System.out.println(getFib(12)); | |
} | |
public static int getFib(int n) | |
{ | |
if (n == 0) return 0; | |
if (n == 1) return 1; | |
Stack<Integer> s = new Stack<Integer>(); | |
s.push(0); | |
s.push(1); | |
for (int i = 2; i <= n; i++) { | |
int num2 = s.pop(); | |
int num1 = s.pop(); | |
int num3 = num1 + num2; | |
s.push(num1); | |
s.push(num2); | |
s.push(num3); | |
} | |
return s.pop(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment