当前位置:网站首页>力扣解法汇总473-火柴拼正方形
力扣解法汇总473-火柴拼正方形
2022-06-12 02:03:00 【失落夏天】
目录链接:
力扣编程题-解法汇总_分享+记录-CSDN博客
GitHub同步刷题项目:
https://github.com/September26/java-algorithms
原题链接:力扣
描述:
你将得到一个整数数组 matchsticks ,其中 matchsticks[i] 是第 i 个火柴棒的长度。你要用 所有的火柴棍 拼成一个正方形。你 不能折断 任何一根火柴棒,但你可以把它们连在一起,而且每根火柴棒必须 使用一次 。
如果你能使这个正方形,则返回 true ,否则返回 false 。
示例 1:
输入: matchsticks = [1,1,2,2,2]
输出: true
解释: 能拼成一个边长为2的正方形,每边两根火柴。
示例 2:
输入: matchsticks = [3,3,3,3,4]
输出: false
解释: 不能用所有火柴拼成一个正方形。
提示:
1 <= matchsticks.length <= 15
1 <= matchsticks[i] <= 108
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/matchsticks-to-square
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路:
* 解题思路: * 首先累加,求出平均数average。 * 然后我们开始递归循环,先从数组中取出0位置的数,放到a1位置。 * 再取出1位置的数,则有两种可能,第一是和a1累加,第二是放到a2位置。 * 再取出2位置的数,则有三种可能,第一是和a1累加,第二是和a2累加,第三是放到a3位置。 * 依次类推,如果a1,a2,a3,a4有任何一个数大于平均数average,则失败。如果index位置等于-1,则证明没问题。 * 当然,针对上面可以有两点优化,第一数预先对数组排序,这样有利于更快的进入false或者true的逻辑。 * 第二是我们可以把a1,a2,a3,a4用数组表示,使用完之后把值改回去即可。
代码:
public class Solution473 {
public boolean makesquare(int[] matchsticks) {
int sum = 0;
for (int i : matchsticks) {
sum += i;
}
if (sum % 4 != 0) {
return false;
}
int average = sum / 4;
Arrays.parallelSort(matchsticks);
return recursion(matchsticks, matchsticks.length - 1, average, 0, 0, 0, 0);
}
private boolean recursion(int[] matchsticks, int index, int average, int a1, int a2, int a3, int a4) {
if (a1 > average || a2 > average || a3 > average || a4 > average) {
return false;
}
if (index == -1) {
return true;
}
int value = matchsticks[index];
index--;
if (a1 == 0) {
return recursion(matchsticks, index, average, value, a2, a3, a4);
}
if (a2 == 0) {
return recursion(matchsticks, index, average, a1 + value, a2, a3, a4) || recursion(matchsticks, index, average, a1, value, a3, a4);
}
if (a3 == 0) {
return recursion(matchsticks, index, average, a1 + value, a2, a3, a4) || recursion(matchsticks, index, average, a1, a2 + value, a3, a4) || recursion(matchsticks, index, average, a1, a2, a3 + value, a4);
}
if (a4 == 0) {
return recursion(matchsticks, index, average, a1 + value, a2, a3, a4) || recursion(matchsticks, index, average, a1, a2 + value, a3, a4) || recursion(matchsticks, index, average, a1, a2, a3 + value, a4) || recursion(matchsticks, index, average, a1, a2, a3, value);
}
return recursion(matchsticks, index, average, a1 + value, a2, a3, a4) || recursion(matchsticks, index, average, a1, a2 + value, a3, a4) || recursion(matchsticks, index, average, a1, a2, a3 + value, a4) || recursion(matchsticks, index, average, a1, a2, a3, a4 + value);
}
}边栏推荐
- In 2022, the internal promotion of the "MIHA Tour" golden, silver and silver social recruitment started in April and march! Less overtime, good welfare, 200+ posts for you to choose, come and see!
- virsh创建/关闭/停止虚拟机常用的几条指令
- [从零开始学习FPGA编程-19]:快速入门篇 - 操作步骤4-1- Verilog 软件下载与开发环境的搭建- Altera Quartus II版本
- PyGame alien invasion
- SQL calculates KS, AUC, IV, psi and other risk control model indicators
- PHP security development 13 column module of blog system
- Graphic data analysis | data cleaning and pretreatment
- On the night of the joint commissioning, I beat up my colleagues
- [learn FPGA programming from scratch -20]: quick start chapter - operation steps 4-2-quick use of Altera quartz II tool (Modelsim co simulation, program download to altera development board)
- Quatre schémas de mise en file d'attente des messages pour redis
猜你喜欢

通用树形结构的迭代与组合模式实现方案

华为联运游戏或应用审核驳回:应用检测到支付serviceCatalog:X6

The establishment and introduction of the announcement module of PHP development blog system

Why do we use Google search ads?

UE4\UE5触摸屏touch事件:单指、双指

matplotlib. pyplot. Bar chart (II)

入手Ticwatch2

如何让杀毒软件停止屏蔽某个网页?以GDATA为例
![[adjustment] in 2022, the Key Laboratory of laser life sciences of the Ministry of education of South China Normal University enrolled adjustment students in optics, electronic information, biomedicin](/img/f9/332b206d5aca0ca6afc3fdf11a53c8.jpg)
[adjustment] in 2022, the Key Laboratory of laser life sciences of the Ministry of education of South China Normal University enrolled adjustment students in optics, electronic information, biomedicin

Does the virtual host have independent IP
随机推荐
[untitled]
联调这夜,我把同事打了...
Alicloud OSS file upload system
C asynchronous programming from simple to deep (III) details awaiter
leetcode:6. Zigzag transformation
ozzanimation-基于sse的动作系统
Ce soir - là, j'ai battu mon collègue...
Software engineering - system flow chart
消防栓监测系统毕业设计---论文(附加最全面的从硬件电路设计->驱动程序设计->阿里云物联网搭建->安卓APP设计)
没有文笔,大家多多包涵
Wide match modifier symbol has been deprecated, do not use
LeetCode Algorithm 997. 找到小镇的法官
Ozzanmation action system based on SSE
In 2022, the internal promotion of the "MIHA Tour" golden, silver and silver social recruitment started in April and march! Less overtime, good welfare, 200+ posts for you to choose, come and see!
[C language] summary of basic knowledge points of pointer
Design principle [Demeter's Law]
On the night of the joint commissioning, I beat up my colleagues
PHP development 09 article module deletion and article classification writing
UE4\UE5触摸屏touch事件:单指、双指
How to maximize the use of various matching methods—— Google SEM