Recursion-Linked Lists

Basics

Estimated reading: 1 minute 35 views
				
					public int bunnyEars2(int bunnies) {
    if (bunnies == 0)
        return 0;
    
    if (bunnies % 2 == 1)
        return 2 + bunnyEars2(bunnies - 1);
    
    return 3 + bunnyEars2(bunnies - 1);
}

				
			
				
					public int count7(int n) {
    if (n == 0)
        return 0;
    
    if (n % 10 == 7)
        return 1 + count7(n / 10);
    
    return count7(n / 10);
}

				
			
				
					public int countX(String str) {
    if (str.length() == 0)
        return 0;
    
    if (str.charAt(0) == 'x')
        return 1 + countX(str.substring(1));
    
    return countX(str.substring(1));
}

				
			
				
					public int fibonacci(int n) {
    if (n <= 1)
        return n;
    
    return fibonacci(n - 1) + fibonacci(n - 2);
}

				
			
				
					public int powerN(int base, int n) {
    if (n <= 1)
        return base;
    
    return base * powerN(base, n - 1);
}

				
			
Share this Doc

Basics

Or copy link

CONTENTS