美文网首页
【2错-2】矩形覆盖

【2错-2】矩形覆盖

作者: 7ccc099f4608 | 来源:发表于2019-01-27 15:39 被阅读3次

https://www.nowcoder.com/practice/72a5a919508a4251859fb2cfb987a0e6?tpId=13&tqId=11163&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
| 日期 | 是否一次通过 | comment |
|----|----|----|
|2019-01-26 13:20|N|实质是斐波那契数列,尾递归实现有点问题|
|2019-01-27 13:20|N|尾递归实现有点问题|

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

1. 递归

public class Solution {
    public int RectCover(int target) {
        if(target <= 2) {
            return target;
        } 
        
        return helper(target);
    }
    
    private int helper(int target) {
        if(target <= 2) {
            return target;
        }
        
        return helper(target-1)+helper(target-2);
    }
}

2.尾递归

public class Solution {
    public int RectCover(int target) {
        if(target <= 2) {
            return target;
        }
         
        return helper(target, 1, 2);
    }
     
    private int helper(int target, int num1, int num2) {
        if(target == 1) {
            return num1;
        }
         
        return helper(target-1, num2, num1+num2);
    }
}

相关文章

  • 【2错-2】矩形覆盖

    https://www.nowcoder.com/practice/72a5a919508a4251859fb2c...

  • 《剑指offer》— JavaScript(10)矩形覆盖

    矩形覆盖 题目描述 我们可以用(2*1)的小矩形横着或者竖着去覆盖更大的矩形。请问用n个(2*1)的小矩形无重叠地...

  • 【python】矩形覆盖?

    题目:我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形...

  • 矩形覆盖

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

  • [剑指offer][Java]矩形覆盖

    题目 我们可以用2×1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2×1的小矩形无重叠地覆盖一个2×n的大矩形...

  • java数据结构和算法(09)矩形覆盖

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

  • 循环-矩形覆盖-java

    循环-矩形覆盖 题目描述 我们可以用2×1 的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2*1的小矩形无重叠地...

  • 剑指Offer上两个一样的题

    1.我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,...

  • 剑指offer--10. 矩形覆盖

    题目:我们可以用2 * 1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2 * 1的小矩形无重叠地覆盖一个2 *...

  • 算法题10.矩形覆盖

    题目描述: 我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2*1的小矩形无重叠地覆盖一个2*n的...

网友评论

      本文标题:【2错-2】矩形覆盖

      本文链接:https://www.haomeiwen.com/subject/obagjqtx.html