Git Product home page Git Product logo

bingonlastship's Introduction

Recursion

We can think about recursion as a special technique for problems that may be hard to solve using loops.

A recursive method calls itself.

Examples from "Java Programming" 10th edition by Y.Daniel Liang

Factorial

Base case, or stopping condition:

0! = 1;

and, recursive call

n! = n x (n - 1)!; n > 0

And so, we can describe the factorial(n) as follows:

if (n == 0)
    return 1;
else
    return n * factorial(n - 1);

Fibbonacci

The Fibonacci series begins with 0 or 1, and each subsequent number is the sum of the preceding two.

public static long fib(long index){
    if (index == 0)
        return 0; // base case
    else if (index == 1) 
        return 1; // base case
    else
        return fib(index-1) + fib(index-2);

}

Problem solving

  • The implementation involves using if-else are switch with different cases
  • The implementation uses one or more base cases
  • Every recursive call involves a recursive call on a smaller problem

And so, problems are broken into subproblems.

The Last Ship Binge

public static void watchLastShip(Series lastShip) {
    if (!lastShip.isEmpty()) {
        lastShip.watchOneEpisode();
        watchLastShip(lastShip);
    }
}

We have two problems to solve:

  • watch one episode
  • watch the rest of the series

The base case is when there are no more episodes to watch.

Palindrome problem

The string is a palindrome if it reads the same from left and right, like "aha", or "a";

The problem can be divided as:

  • Look at the first and the last character of the string. Compare, if they are the same continue
  • Ignore the two characters you just checked, continue
public class PalindromeCheck {
    public static boolean isPalindrome(String str){
        if (str.length() <= 1) return true;
        else if (str.charAt(0) != str.charAt(str.length() - 1)) return false;
        else return isPalindrome(s.substring(1, s.length() -1 ));
    }

}

bingonlastship's People

Contributors

bulldogslucky13 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.