js- brush prove safety record title (and recursive loop)

1. Fibonacci number

We all know that Fibonacci number, and now asked to enter an integer n, you output the n-th Fibonacci number Fibonacci sequence of item (from 0, the first 0 is 0). n <= 39

My solution

Recursion, in vscode run a bit is no problem, but in the cattle off the Internet on the run too long. Operation is too big, stack overflow.

function Fibonacci(n)
{
    if(n == 0) return 0
    if(n == 1) return 1
    if(n>=2&&n<=39){
        let x = Fibonacci(n-1)
        let y = Fibonacci(n-2)
        return x+y
    }
    return null
}

Other analysis

Solution to a problem that is mentioned in a lot of ways, dynamic programming, curried, tail recursion.
Dynamic Programming
force wear article, written very clearly
https://leetcode-cn.com/problems/fibonacci-number/solution/dong-tai-gui-hua-tao-lu-xiang-jie-by-labuladong/

function Fibonacci(n)
{
    if(n == 0) return 0
    if(n == 1) return 1
    let pre = 0
    let cur = 1
    for(let i=2;i<=n;i++){
        let sum = pre + cur
        pre = cur
        cur = sum
    }
    return cur
}

2. jump stairs

A frog can jump on a Class 1 level, you can also hop on level 2. The frog jumped seeking a total of n grade level how many jumps (the order of different calculation different results).

My solution

In fact, the above is that question, This question is a good solution.
N = jumped jumped stairs stairs n-1 + jump a jump on n-2 to jump two stairs
this is not a variant Fibonacci columns Well, the use of dynamic programming can be written, Of course, it can also recursive

function jumpFloor(number)
{
    if(number == 1) return 1
    if(number == 2) return 2
    let pre = 1
    let cur = 2
    for(let i=3;i<=number;i++){
        let sum = pre+cur
        pre = cur
        cur = sum
    }
    return cur
}

3. metamorphosis jump stairs

A frog can jump on a Class 1 level, you can also hop on level 2 ...... n It can also jump on stage. The frog jumped seeking a total of n grade level how many jumps.

My solution

Frog exhausted. This question is not as algorithm problems, like a math problem.
Jump n stages f (n) = f (n -1) + f (n-2) + ... + f (1)
jump stage n-1 f (n-1) = f (n-2) + f (n-3) + ... + f (1)
phase subtraction f (n) = 2f (n -1)

function jumpFloorII(number)
{
    if(number == 1) return 1
    let i = 1
    while(--number){//注意这里是--number
        i*=2
    }
    return i
}

Supplement

--aIs pre-decrement type, the return value after decrement
a--is post-decrement type, return the value before decrementing

  var a = 3
  var c = a--
  console.log(a);  //2
  console.log(c);  //3
  

  var b = 3
  var d = --b
  console.log(b);  //2
  console.log(d);  //2

Therefore, while the above-described code number is determined after the decrement value, as the number of cycles n-1 times, if the number- written, the loop n times is

4. rectangular footprint

We can use 2 small rectangle 1 of sideways or vertically to cover a larger rectangle. Will the use of n 2 small rectangle 1 coverage without overlap a large rectangle 2 * n, a total of how many ways?
For example when n = 3, 2 * 3 tile is covered there are three methodsHere Insert Picture Description

My solution

This question may naturally think 2 the n-rectangle is 2 (the n--1) rectangle plus one, certainly a bit like a Fibonacci number, then that is drawing summarized. Can be derived f (n) = f (n -1) + f (n-2), of course, consider the case where n is 1 and 2

function rectCover(number)
{
    if(number == 0) return 0
    if(number == 1) return 1
    if(number == 2) return 2
    let pre = 1
    let cur = 2
    for (let i=3;i<=number;i++){
        let sum = pre+cur
         pre = cur
         cur = sum
    }
    return cur
}
Published 21 original articles · won praise 0 · Views 179

Guess you like

Origin blog.csdn.net/adrenalineiszzz/article/details/104826122
Recommended