Cattle brush off net 30 (two questions) questions

55. The rectangular footprint

Topic Link

https://www.nowcoder.com/practice/72a5a919508a4251859fb2cfb987a0e6?tpId=13&&tqId=11163&rp=2&ru=/activity/oj&qru=/ta/coding-interviews/question-ranking

Title Description

We can use a small rectangle 2 * 1 sideways or vertically to cover a larger rectangle. Will the small rectangle of n 2 * 1 coverage without overlap a large rectangle 2 * n, a total of how many ways?
 
For n = 3 for example, 2 * 3 tile is covered There are three methods:

 

 

 

Heavy and difficult

Amount Fibonacci number sequence recursion formulas: f (n) = f (n-1) + f (n-2)

Topic analysis

    1. n = 1: 1 Species, n = 2: 2 Species, n = 3: 3 species.
    2. Starting from n = 3, f (n) = f (n-1) + f (n-2).
function rectCover(number)
{
    if(number < 3){
        return number;
    }else{
        var f1 = 1;
        var f2 = 2;
        var f3;
        for(var i=3;i<=number;i++){
            f3 = f1 + f2;
            f1 = f2;
            f2 = f3;
        }
        return f3;
    }
}

55. seeking 1 + 2 + 3 + ... + n

Topic Link

https://www.nowcoder.com/practice/7a0da8fc483247ff8800059e12d7caf1?tpId=13&&tqId=11200&rp=4&ru=/activity/oj&qru=/ta/coding-interviews/question-ranking

Title Description

Seeking 1 + 2 + 3 + ... + n, requires multiplication and division can not be used, for, while, if, else, switch, case and keywords such as conditional statement (A B:? C).

Heavy and difficult

  1. Arithmetic Progression: sum = (1 + n) * n / 2;
  2. Right: n / 2 n >> 1 can be used instead.

Topic analysis

  1. The series summation formula Found: sum = (1 + n) * n / 2;
  2. Multiplication and division can not be used, then this formula can be expressed as: sum = (n ^ 2 + n) / 2 = (n ^ 2 + n) >> 1
function Sum_Solution(n)
{
    var res = Math.pow(n,2)+n;
    return res >> 1;
}

  

Guess you like

Origin www.cnblogs.com/liu-xin1995/p/12387761.html