前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >leetcode473. Matchsticks to Square

leetcode473. Matchsticks to Square

作者头像
眯眯眼的猫头鹰
发布2019-11-03 12:32:38
2760
发布2019-11-03 12:32:38
举报
文章被收录于专栏:眯眯眼猫头鹰的小树杈

题目要求

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.

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

思路和代码

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

代码语言:javascript
复制
    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;

    }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目要求
  • 思路和代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档