# 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]区间中的整数没有出现？

• ### Leetcode 26. Remove Duplicates from Sorted Array

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

• ### leetcode398. Random Pick Index

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

• ### LWC 54：697. Degree of an Array

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

• ### 堆与栈区别

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