剑指offer--矩形覆盖

题目描述

我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?

分类:数组

解法1:斐波那契数列
[java]  view plain  copy
  1. public class Solution {  
  2.     public int RectCover(int target) {  
  3.         if(target==1return 1;  
  4.         if(target==2return 2;  
  5.         return RectCover(target-1)+RectCover(target-2);  
  6.     }  
  7. }  

下面是迭代算法,用空间换时间

[java]  view plain  copy
  1. public class Solution {  
  2.     public int RectCover(int target) {  
  3.         if(target==0||target==1return 1;    
  4.         if(target==2return 2;    
  5.         int pre  = 1;    
  6.         int next = 2;    
  7.         int t = 0;    
  8.         int i =2;    
  9.         while(i<target){    
  10.             t = pre+next;    
  11.             pre = next;    
  12.             next = t;    
  13.             i++;    
  14.         }           
  15.         return next;    
  16.     }  
  17. }  

原文链接  http://blog.csdn.net/crazy__chen/article/details/44985287

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值