/
Solution.java
37 lines (34 loc) · 1.06 KB
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.nateshao.sword_offer.topic_11_RectCover;
/**
* @date Created by 邵桐杰 on 2021/11/20 11:26
* @微信公众号 千羽的编程时光
* @个人网站 www.nateshao.cn
* @博客 https://nateshao.gitee.io
* @GitHub https://github.com/nateshao
* @Gitee https://gitee.com/nateshao
* Description:
*/
public class Solution {
public static void main(String[] args) {
int rectCover = rectCover(3);
System.out.println("rectCover = " + rectCover);
int rectCover2 = rectCover2(3);
System.out.println("rectCover2 = " + rectCover2);
}
public static int rectCover(int target) {
if (target <= 0) return 0;
else if (target <= 2) return target;
else return rectCover(target - 1) + rectCover(target - 2);
}
public static int rectCover2(int target) {
int a = 1, b = 2, sum = 0;
if (target == 0) return 0;
if (target <= 2) return target;
for (int i = 3; i <= target; i++) {
sum = a + b;
a = b;
b = sum;
}
return sum;
}
}