当前位置:网站首页>Force deduction solution summary 473 match to square
Force deduction solution summary 473 match to square
2022-06-12 02:09:00 【Lost summer】
Directory links :
Force buckle programming problem - The solution sums up _ Share + Record -CSDN Blog
GitHub Synchronous question brushing items :
https://github.com/September26/java-algorithms
Original link : Power button
describe :
You will get an array of integers matchsticks , among matchsticks[i] It's No i The length of a matchstick . Do you want us to use All the matchsticks Make a square . you Can't break Any matchstick , But you can connect them together , And every matchstick must Use it once .
If you can make this square , Then return to true , Otherwise return to false .
Example 1:
Input : matchsticks = [1,1,2,2,2]
Output : true
explain : Can be put together into a side length of 2 The square of , Two matches on each side .
Example 2:
Input : matchsticks = [3,3,3,3,4]
Output : false
explain : You can't make a square out of all the matches .
Tips :
1 <= matchsticks.length <= 15
1 <= matchsticks[i] <= 108
source : Power button (LeetCode)
link :https://leetcode.cn/problems/matchsticks-to-square
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Their thinking :
* Their thinking : * First, add up , Find the average average. * Then we start the recursive loop , First, take out from the array 0 The number of positions , Put it in a1 Location . * Take it out again 1 The number of positions , There are two possibilities , The first is and a1 Add up , The second is to put a2 Location . * Take it out again 2 The number of positions , There are three possibilities , The first is and a1 Add up , The second is and a2 Add up , The third is to put a3 Location . * By analogy , If a1,a2,a3,a4 Any number is greater than the average average, The failure . If index The position is equal to -1, Then it proves that there is no problem . * Of course , There are two optimizations for the above , The first number sorts the array in advance , This is conducive to faster access false perhaps true The logic of . * The second is that we can put a1,a2,a3,a4 Express in an array , Change the value back after use .
Code :
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);
}
}边栏推荐
- 力扣解法汇总433-最小基因变化
- ozzanimation-基於sse的動作系統
- PHP builds a high-performance API architecture based on sw-x framework (III)
- 力扣解法汇总358-迷你语法分析器
- Implementation scheme of iteration and combination pattern for general tree structure
- 力扣解法汇总473-火柴拼正方形
- Leetcode 55 jump game
- 超图倾斜数据合并根节点后转3dtiles
- Force deduction solution summary 824 goat Latin
- What are the preparations for setting up Google search advertising series?
猜你喜欢

Three main factors determining advertising quality

Is there a female Bluetooth headset suitable for girls? 38 Bluetooth headsets worth getting started

The most comprehensive redis transaction control in 2022 (with illustration)

Installing MySQL version 5.5 database for Linux (centos6)

Why do we use Google search ads?

决定广告质量的三个主要因素

How to automatically color cells in Excel

消防栓监测系统毕业设计---论文(附加最全面的从硬件电路设计->驱动程序设计->阿里云物联网搭建->安卓APP设计)
![[adjustment] notice on the opening of the 2022 pre adjustment system for postgraduate enrollment of Shanghai Second University of Technology](/img/16/2f9a235995cdd54ac9b85a68a7afcb.jpg)
[adjustment] notice on the opening of the 2022 pre adjustment system for postgraduate enrollment of Shanghai Second University of Technology

Basedexclassloader
随机推荐
Design principle [Demeter's Law]
Google Ads 的优势
SwiftyJSON解析本地JSON文件
Metaverse × How will smart cities develop?
通过搜索广告附加信息让广告更具相关性
力扣解法汇总-04.06. 后继者
力扣解法汇总473-火柴拼正方形
The most comprehensive redis transaction control in 2022 (with illustration)
How to automatically color cells in Excel
Force deduction solution summary 668- the smallest number k in the multiplication table
Is the bidding price fixed for each click?
Swiftyjson analyse les fichiers json locaux
通用树形结构的迭代与组合模式实现方案
Three main factors determining advertising quality
PHP security development 13 column module of blog system
2022福建省安全员C证(专职安全员)考试模拟100题及答案
How to maximize the use of various matching methods—— Google SEM
What are the advantages of adaptive search advertising?
virsh创建/关闭/停止虚拟机常用的几条指令
How to locate keywords to make advertising accurate.