题目
你将得到一个整数数组 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
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解决方法
fun makesquare(matchsticks: IntArray): Boolean {val sum = matchsticks.sum()if (sum % 4 != 0) {return false}val edges = IntArray(4) { 0 }//为什么逆序排了一下 效率这么高?matchsticks.sortDescending()return dfs(0, matchsticks, edges, sum / 4)}private fun dfs(index: Int, matchsticks: IntArray, edges: IntArray, length: Int): Boolean {if (index == matchsticks.size) {return true}for (i in 0..3) {if (edges[i] + matchsticks[index] > length) {continue}edges[i] += matchsticks[index]if (dfs(index + 1, matchsticks, edges, length)) {return true}edges[i] -= matchsticks[index]}return false}
总结
1.算法就是把不同的问题,转换成熟悉的模型去解决。要学会数学归纳
2.DFS真的是一个必须掌握的
3.日子一天一天过,没有特别精彩,也没有特别难过