这个貌似就是普通的跳台阶问题。
方法一:递归法
class Solution { public int rectCover(int number) { if(number <= 0) { return 0; } if(number == 1) { return 1; } if(number == 2) { return 2; } return rectCover(target - 1) + rectCover(target -2 ); } }
方法二:循环法
class Solution { public int rectCover(int number) { // write code here int sum = 0; int a=1,b=2; if (number == 1) { sum = 1; } else if (number==2) { sum = 2; } else { for (int i = 3; i <=number; i++) { sum = a + b; a = b; b = sum; } } return sum; } }
原文:https://www.cnblogs.com/WeiMLing/p/10886587.html