leetcode在力扣 App 中打开
调试中...
调试中...
题目描述
题目描述
题解
题解
提交记录
提交记录
代码
代码
测试用例
测试用例
测试结果
测试结果
中等
相关标签
相关企业
提示

你将得到一个整数数组 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
通过次数
83.4K
提交次数
178K
通过率
46.8%


相关企业

提示 1
Treat the matchsticks as an array. Can we split the array into 4 equal parts?

提示 2
Every matchstick can belong to either of the 4 sides. We don't know which one. Maybe try out all options!

提示 3
For every matchstick, we have to try out each of the 4 options i.e. which side it can belong to. We can make use of recursion for this.

提示 4
We don't really need to keep track of which matchsticks belong to a particular side during recursion. We just need to keep track of the length of each of the 4 sides.

提示 5
When all matchsticks have been used we simply need to see the length of all 4 sides. If they're equal, we have a square on our hands!

相似题目

评论 (0)

贡献者
© 2025 领扣网络(上海)有限公司
0 人在线
行 1,列 1
运行和提交代码需要登录
matchsticks =
[1,1,2,2,2]
Source