专栏首页眯眯眼猫头鹰的小树杈leetcode473. Matchsticks to Square

leetcode473. Matchsticks to Square

题目要求

Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match girl has, please find out a way you can make one square by using up all those matchsticks. You should not break any stick, but you can link them up, and each matchstick must be used exactly one time.

Your input will be several matchsticks the girl has, represented with their stick length. Your output will either be true or false, to represent whether you could make one square using all the matchsticks the little match girl has.

Example 1:

Input: [1,1,2,2,2] Output: true

Explanation: You can form a square with length 2, one side of the square came two sticks with length 1.

Example 2:

Input: [3,3,3,3,4] Output: false

Explanation: You cannot find a way to form a square with all the matchsticks.

Note:

  1. The length sum of the given matchsticks is in the range of 0 to 10^9.
  2. The length of the given matchstick array will not exceed 15.

现有一个整数数组代表一堆各种长度的火柴。问这些火柴拼拼凑凑是否能够拼成一个正方形。要求每根火柴只能使用一次。

思路和代码

这里采用的是深度优先遍历的思路,即尝试将木棍分别放在每个边上,看看是否能够最终构成一个正方形。一个简单的优化方式是减少重复长度的火柴的分配。

    public boolean makesquare(int[] nums) {
        if (nums == null || nums.length == 0){
            return false;
        }
        int sum = 0;
        int max = 0;
        for (int num : nums) {
            sum += num;
            max = Math.max(num, max);
        }
        if (sum % 4 != 0) {
            return false;
        }
        int sideLength = sum / 4;
        if (max > sideLength) {
            return false;
        }
        return makesquare(nums, 0, sideLength, sideLength, sideLength, sideLength);
    }

    public boolean makesquare(int[] nums, int index, int firstSide, int secondSide, int thirdSide, int fourthSide) {
        if (index >= nums.length) return true;
        boolean canMake = false;
        if (firstSide >= nums[index]) {
            firstSide -= nums[index];
            canMake = makesquare(nums, index+1, firstSide, secondSide, thirdSide, fourthSide);
            firstSide += nums[index];
        }

        if (!canMake && secondSide != firstSide && secondSide >= nums[index]) {
            secondSide -= nums[index];
            canMake = makesquare(nums, index+1, firstSide, secondSide, thirdSide, fourthSide);
            secondSide += nums[index];
        }

        if (!canMake && thirdSide != secondSide && thirdSide >= nums[index]) {
            thirdSide -= nums[index];
            canMake = makesquare(nums, index+1, firstSide, secondSide, thirdSide, fourthSide);
            thirdSide += nums[index];
        }

        if (!canMake && fourthSide != thirdSide && fourthSide >= nums[index]) {
            fourthSide -= nums[index];
            canMake = makesquare(nums, index+1, firstSide, secondSide, thirdSide, fourthSide);
            fourthSide += nums[index];
        }
        return canMake;

    }

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • leetcode398. Random Pick Index

    设计一个数据结构,使得从该数据结构中查询一个数字时,能够以等概率返回该数字所在的任何下标。额外的要求是只要占用O(1)的额外的空间复杂度。

    眯眯眼的猫头鹰
  • leetcode442. Find All Duplicates in an Array

    存在一个整数数组,其中的所有元素都位于1~n之间,其中n是数组的长度。有的元素出现了一次,而有的元素出现了两次。找到数组中所有出现两次的数字。

    眯眯眼的猫头鹰
  • leetcode448. Find All Numbers Disappeared in an Array

    假设一个长度为n的整数数组,数组中的元素的值位于[1,n]区间中。问,该数组中有哪些[1,n]区间中的整数没有出现?

    眯眯眼的猫头鹰
  • Remove Duplicates from Sorted Array II

    Tyan
  • Leetcode 26. Remove Duplicates from Sorted Array

    版权声明:博客文章都是作者辛苦整理的,转载请注明出处,谢谢! https://blog.csdn....

    Tyan
  • leetcode398. Random Pick Index

    设计一个数据结构,使得从该数据结构中查询一个数字时,能够以等概率返回该数字所在的任何下标。额外的要求是只要占用O(1)的额外的空间复杂度。

    眯眯眼的猫头鹰
  • leetcode 16 3Sum Closest

    @坤的
  • LWC 54:697. Degree of an Array

    LWC 54:697. Degree of an Array 传送门:697. Degree of an Array Problem: Given a non...

    用户1147447
  • leetcode 34 Search for a Range

    @坤的
  • 堆与栈区别

    堆(Heap)与栈(Stack)是开发人员必须面对的两个概念,在理解这两个概念时,需要放到具体的场景下,因为不同场景下,堆与栈代表不同的含义。一般情况下,有两层...

    Anymarvel

扫码关注云+社区

领取腾讯云代金券