473. 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:
- The length sum of the given matchsticks is in the range of
0
to10^9
.- The length of the given matchstick array will not exceed
15
.
Approach #1: DFS. [Java]
class Solution {
public boolean makesquare(int[] nums) {
if (nums == null || nums.length < 4) return false; int sum = 0;
for (int num : nums)
sum += num;
if (sum % 4 != 0) return false;
int side = sum / 4; int[] sides = {0, 0, 0, 0}; Arrays.sort(nums);
reverse(nums); return dfs(sides, nums, 0, side);
} public boolean dfs(int[] sides, int[] nums, int index, int side) {
if (index == nums.length) {
if (sides[0] == side && sides[1] == side && sides[2] == side)
return true;
return false;
}
for (int i = 0; i < 4; ++i) {
if (sides[i] + nums[index] > side) continue;
sides[i] += nums[index];
if (dfs(sides, nums, index+1, side)) return true;
sides[i] -= nums[index];
}
return false;
} public void reverse(int[] nums) {
int l = 0, r = nums.length - 1;
while (l < r) {
int temp = nums[l];
nums[l] = nums[r];
nums[r] = temp;
l++;
r--;
}
}
}
Analysis:
Every matchstack can belong to either of the 4 sides. We don't which one. Maybe try out every options.
Reference:
https://leetcode.com/problems/matchsticks-to-square/discuss/95729/Java-DFS-Solution-with-Explanation
473. Matchsticks to Square的更多相关文章
- 【LeetCode】473. Matchsticks to Square 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- LeetCode "473. Matchsticks to Square"
A trickier DFS, with a little bit complex recursion param tweak, and what's more important is prunin ...
- 473 Matchsticks to Square 火柴拼正方形
还记得童话<卖火柴的小女孩>吗?现在,你知道小女孩有多少根火柴,请找出一种能使用所有火柴拼成一个正方形的方法.不能折断火柴,可以把火柴连接起来,并且每根火柴都要用到.输入为小女孩拥有火柴的 ...
- 【leetcode】473. Matchsticks to Square
题目如下: 解题思路:居然把卖火柴的小女孩都搬出来了.题目的意思是输入一个数组,判断能否把数组分成四个子数组,使得每个子数组的和相等.首先我们可以很容易的求出每个子数组的和应该是avg = sum(n ...
- Leetcode之深度优先搜索(DFS)专题-473. 火柴拼正方形(Matchsticks to Square)
Leetcode之深度优先搜索(DFS)专题-473. 火柴拼正方形(Matchsticks to Square) 深度优先搜索的解题详细介绍,点击 还记得童话<卖火柴的小女孩>吗?现在, ...
- [LeetCode] Matchsticks to Square 火柴棍组成正方形
Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...
- Leetcode: Matchsticks to Square && Grammar: reverse an primative array
Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...
- [Swift]LeetCode473. 火柴拼正方形 | Matchsticks to Square
Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...
- leetcode473 Matchsticks to Square
一开始想求所有结果为target的组合来着,但是所选元素不能重叠.用这个递归思想很简单,分成四个桶,每次把元素放在任意一个桶里面,最后如果四个桶相等就可以放进去,有一个地方可以剪枝,假如任意一个桶的元 ...
随机推荐
- Java基本概念:多态
一.简介 描述: 多态性是面向对象编程中的一个重要特性,主要是用来实现动态联编的.换句话说,就是程序的最终状态只有在执行过程中才被决定,而非在编译期间就决定了.这对于大型系统来说能提高系统的灵活性和扩 ...
- Kubernetes-3.安装
docker version:19.03.14 kubernetes version:1.19.4 本文介绍使用kubeadm安装Kubernetes集群的简单过程. 目录 使用kubeadm安装k8 ...
- 后端程序员之路 1、linux、centos
CentOS.Ubuntu.Debian三个linux比较异同 - 记事本 - 博客频道 - CSDN.NEThttp://blog.csdn.net/educast/article/details/ ...
- Docker安装Openvas
目录 安装 在本机内运行 在局域网内运行 关闭 参考 安装 ➜ ~ docker search openvas NAME DESCRIPTION STARS OFFICIAL AUTOMATED mi ...
- MySQL全面瓦解24:构建高性能索引(策略篇)
学习如果构建高性能的索引之前,我们先来了解下之前的知识,以下两篇是基础原理,了解之后,对面后续索引构建的原则和优化方法会有更清晰的理解: MySQL全面瓦解22:索引的介绍和原理分析 MySQL全面瓦 ...
- 【HTB系列】靶机Bitlab的渗透测试
出品|MS08067实验室(www.ms08067.com) 本文作者:是大方子(Ms08067实验室核心成员) 0x00 本文目录 反思与总结 基本信息 渗透测试过程 补充 0x01 反思与总结 c ...
- RabbitMQ简介、安装、基本特性API--Java测试
新的阅读体验地址:http://www.zhouhong.icu/post/141 本篇文章所有的代码:https://github.com/Tom-shushu/Distributed-system ...
- 记录一个在配置虚拟环境是遇到的错误(virtualenv)
原配置文件 export WORKON_HOME=~/Envs #设置virtualenv的统一管理目录 export VIRTUALENVWRAPPER_VIRTUALENV_ARGS='--no- ...
- 【odoo14】第二章、管理odoo实例
本章主要介绍肖odoo实例添加用户自定义的模块.你可以从多个路径载入模块.但是建议你将自己的模块儿放在特定的目录当中,避免与odoo的核心模块混淆. 在这一章节,中我们主要涉及以下内容: 配置插件路径 ...
- Blind Super-Resolution Kernel Estimation using an Internal-GAN 论文解读
背景与思路来源 目前 SR 模型中合成 LR 使用的模糊核问题 目前大多数 SR 的 model 都是用的合成下采样图片来进行训练的,而这些合成的图片常常使用的是 MATLAB 里面的 imresiz ...