题目
我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?
链接
答案
一个:1
二个:2
三个:1 + 2
...
N个:f(n-1) + f(n-2)
代码
1 class Solution { 2 public: 3 int rectCover(int number) { 4 if(number == 0){ 5 return 0; 6 } 7 if(number == 1){ 8 return 1; 9 } 10 11 if(number == 2){ 12 return 2; 13 } 14 15 int next; 16 int first = 1; 17 int second = 2; 18 for(int index = 3; index <= number; ++ index){ 19 next = first + second; 20 first = second; 21 second = next; 22 } 23 24 return next; 25 } 26 };
浙公网安备 33010602011771号