- Fibonacci
function Fibonacci(n) {
if (n <= 0) {
return 0
} else if (n === 1) {
return 1
} else {
return Fibonacci(n - 1) + Fibonacci(n - 2)
}
}
- 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)
}
}
- 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)
}
}
- 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)
}
}
网友评论