Recursion

作者: Oriharas | 来源:发表于2018-12-27 20:32 被阅读0次
  1. Fibonacci
function Fibonacci(n) {
    if (n <= 0) {
        return 0
    } else if (n === 1) {
        return 1
    } else {
        return Fibonacci(n - 1) + Fibonacci(n - 2)
    }
}
  1. A frog can jump one or two steps at a time. How many jumping methods are there when the frog jumps up an n-step? (Different order corresponds to different results)
function jumpFloor(number) {
    if (number <= 0) {
        return 0
    } else if (number === 1) {
        return 1
    } else if (number === 2) {
        return 2
    } else {
        return jumpFloor(number - 1) + jumpFloor(number - 2)
    }
}
  1. A frog can jump up a step or two at a time... It can also jump to level n at a time. Find out how many jumping methods the frog can use to jump up an n-step.
function jumpFloorII(number) {
    if (number <= 0) {
        return 0
    } else if (number === 1) {
        return 1
    } else {
        return 2 * jumpFloorII(number - 1)
    }
}
  1. We can use a small rectangle of 2 * 1 to cover larger rectangles horizontally or vertically. How many ways are there to cover a large 2 * n rectangle with n small 2 * 1 rectangles without overlapping?
function rectCover(number) {
    if (number <= 0) {
        return 0
    } else if (number === 1) {
        return 1
    } else if (number === 2) {
        return 2
    } else {
        return rectCover(number - 1) + rectCover(number - 2)
    }
}

相关文章

  • Binary Tree and Recursion

    DFS Non-recursion (use stack) Recursion (自己调用自己)-- Traver...

  • Recursion

    How to calculate the complexity? How about the space?

  • Recursion

    Fibonacci Find the maximum value among array elements Bub...

  • Recursion

    发自简书 递归 导致递归的方法返回而没有再一次进行递归调用,此时我们称为基值情况( base case)。每一个递...

  • Recursion

    有限记忆和无限思维之间的矛盾促生了语言的递归性 语言是用有限手段生成无限话语的装置.如果一种语法没有递归机制,它将...

  • Recursion

    Fibonacci A frog can jump one or two steps at a time. How...

  • recursion

    22 Generate Parentheses 39 Combination Sum 40 Combination...

  • 递归

    recursion完成了iteration,但逻辑清晰,有以下问题: recursion 由stack完成,会溢出...

  • ZXAlgorithm - C5 DFS

    OutlineRecursionCombinationPermutationGraphNon-recursion ...

  • 递归(recursion)

    基线条件(base case)&递归条件(recursive case) 递归条件基线条件 堆栈 调用栈 递归调用栈

网友评论

      本文标题:Recursion

      本文链接:https://www.haomeiwen.com/subject/hfwdlqtx.html