剑指Offer7
矩形覆盖
https://www.nowcoder.com/practice/72a5a919508a4251859fb2cfb987a0e6?tpId=13&tqId=11163&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking&from=cyc_github
Question
思路
- 从n=1 的情况开始分析,寻找规律,发现是递归问题;
- 递归公式如下:
Code
使用循环方式进行,效率更高class Solution { public: int rectCover(int number) { if(number <= 2) { return number; } int pre1 = 2, pre2 = 1 , res = 0; for(int i = 3; i <= number; i++) { res = pre1 + pre2; pre2 = pre1; //注意先后顺序 pre1 = res; } return res; } };
```