

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


15-0: Recursion
15-1: Recursion – Zero Digits
15-2: Recursion – Zero Digits
int numZeroDigits(int n) { if (n < 10) { if (n == 0) return 1; else return 0; } if (n % 10 == 0) return 1 + numZeroDigits(n / 10); else return numZeroDigits(n / 10); }
What if n is negative? 15-3: Recursion – Zero Digits
int numZeroDigits(int n) { if (n < 0) return numZeroDigits(0 - n); if (n < 10) { if (n == 0) return 1; else return 0; } if (n % 10 == 0) return 1 + numZeroDigits(n / 10); else return numZeroDigits(n / 10); }
What if n is negative? 15-4: Recursion – Change Pi
changePi("pine") ==> "3.14ne" changePi("ppippi") ==> "p3.14p3.14" changePi("nop ie") ==> "p3.14p3.14"
15-5: Recursion – Change Pi
public String changePi(String str) { if ((str.length()) < 2) { return str; } else if (str.substring(0,2).equals("pi")) { return "3.14" + changePi(str.substring(2)); } else { } return str.charAt(0) + changePi(str.substring(1)); }
15-6: Backtracking
15-7: Backtracking - 8 Queens
15-8: Backtracking - 8 Queens
15-9: Backtracking - 8 Queens
15-10: Backtracking - 8 Queens