Skip to content

Instantly share code, notes, and snippets.

@AlexAbraham1
Created January 2, 2015 15:50
Show Gist options
  • Save AlexAbraham1/304ea71ba7e3270df780 to your computer and use it in GitHub Desktop.
Save AlexAbraham1/304ea71ba7e3270df780 to your computer and use it in GitHub Desktop.
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