55. The rectangular footprint
Topic Link
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
- n = 1: 1 Species, n = 2: 2 Species, n = 3: 3 species.
- 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
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
- Arithmetic Progression: sum = (1 + n) * n / 2;
- Right: n / 2 n >> 1 can be used instead.
Topic analysis
- The series summation formula Found: sum = (1 + n) * n / 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; }