题目
我们可以用21的小矩形横着或者竖着去覆盖更大的矩形。请问用n个21的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?
分析
代码
递归实现
public class Solution13 {
public static void main(String
[] args
) {
System
.out
.println(RectCover(4));
}
public static int RectCover(int target
) {
if (target
== 0 || target
== 1){
return target
;
}
return RectCover(target
-1)+RectCover(target
-2);
}
}
循环实现
public class Solution13 {
public static void main(String
[] args
) {
System
.out
.println(RectCover(4));
}
public static int RectCover(int target
) {
if (target
== 0 || target
== 1 || target
== 2){
return target
;
}
int a
= 0; int b
= 1;
for(int i
= 0; i
<= target
; i
++){
int sum
= a
+ b
;
a
= b
;
b
= sum
;
}
return a
;
}
}
转载请注明原文地址:https://tech.qufami.com/read-28943.html