★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10347635.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

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: trueExplanation: 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: falseExplanation: You cannot find a way to form a square with all the matchsticks. 

Note:

  1. The length sum of the given matchsticks is in the range of 0 to 10^9.
  2. The length of the given matchstick array will not exceed 15.

还记得童话《卖火柴的小女孩》吗?现在,你知道小女孩有多少根火柴,请找出一种能使用所有火柴拼成一个正方形的方法。不能折断火柴,可以把火柴连接起来,并且每根火柴都要用到。

输入为小女孩拥有火柴的数目,每根火柴用其长度表示。输出即为是否能用所有的火柴拼成正方形。

示例 1:

输入: [1,1,2,2,2]
输出: true解释: 能拼成一个边长为2的正方形,每边两根火柴。

示例 2:

输入: [3,3,3,3,4]
输出: false解释: 不能用所有火柴拼成一个正方形。

注意:

  1. 给定的火柴长度和在 0 到 10^9之间。
  2. 火柴数组的长度不超过15。

Runtime: 836 ms
Memory Usage: 3.9 MB
 1 class Solution {
 2     func makesquare(_ nums: [Int]) -> Bool {
 3         if nums.isEmpty || nums.count < 4 {return false}
 4         var sum:Int = nums.reduce(0, +)
 5         if sum % 4 != 0 {return false}
 6         var n:Int = nums.count
 7         var all:Int = (1 << n) - 1
 8         var target:Int = sum / 4
 9         var masks:[Int] = [Int]()
10         var validHalf:[Bool] = [Bool](repeating:false,count:1 << n)
11         for i in 0...all
12         {
13             var curSum:Int = 0
14             for j in 0...15
15             {
16                 if ((i >> j) & 1) == 1
17                 {
18                     curSum += nums[j]
19                 }
20             }
21             if curSum == target
22             {
23                 for mask in masks
24                 {
25                     if (mask & i) != 0 {continue}
26                     var half:Int = mask | i
27                     validHalf[half] = true
28                     if validHalf[all ^ half] {return true}
29                 }
30                 masks.append(i)
31             }
32         }
33         return false
34     }
35 }


3911 kb

 1 class Solution {
 2     func makesquare(_ nums: [Int]) -> Bool {
 3         guard  nums.count >= 4 else { return false }
 4         let sum = nums.reduce(0, { $0 + $1 })
 5         if sum % 4 != 0 {
 6             return false
 7         }
 8         var sides: [Int] = Array(repeating: 0, count: 4)
 9         return dfs(nums.sorted(by: >), 0, &sides, sum / 4)
10     }
11
12     func dfs(_ nums: [Int], _ index: Int, _ sides: inout [Int], _ target: Int) -> Bool {
13         if index == nums.count {
14             return sides[0] == sides[1] && sides[0] == sides[2] && sides[0] == sides[3] && sides[0] == target
15         }
16
17         for i in 0 ..< sides.count {
18             if sides[i] + nums[index] > target {
19                 continue
20             }
21             sides[i] += nums[index]
22             if dfs(nums, index + 1, &sides, target) {
23                 return true
24             }
25             sides[i] -= nums[index]
26         }
27         return false
28     }
29 }

转载于:https://www.cnblogs.com/strengthen/p/10347635.html

[Swift]LeetCode473. 火柴拼正方形 | Matchsticks to Square相关推荐

  1. LeetCode473. 火柴拼正方形

    目录 文章目录 一.题目 二.解题思路 三.知识总结 1.C++ sort()排序函数用法详解 2.accumulate()函数 总结 一.题目 你将得到一个整数数组 matchsticks ,其中 ...

  2. 经典回溯之火柴拼正方形

    473. 火柴拼正方形 给定很多小短火柴,拼成一个正方形 用到的技巧 1.  排序,传参数,起到剪枝的效果,排序后前面搜索过的在下层时直接跳过,具体体现为 for (int i = idx;i< ...

  3. LeetCode 473. 火柴拼正方形

    473. 火柴拼正方形 还记得童话<卖火柴的小女孩>吗?现在,你知道小女孩有多少根火柴,请找出一种能使用所有火柴拼成一个正方形的方法.不能折断火柴,可以把火柴连接起来,并且每根火柴都要用到 ...

  4. 算法----火柴拼正方形

    题目 你将得到一个整数数组 matchsticks ,其中 matchsticks[i] 是第 i 个火柴棒的长度.你要用 所有的火柴棍 拼成一个正方形.你 不能折断 任何一根火柴棒,但你可以把它们连 ...

  5. 【Leetcode刷题Python】473. 火柴拼正方形

    1 题目 你将得到一个整数数组 matchsticks ,其中 matchsticks[i] 是第 i 个火柴棒的长度.你要用 所有的火柴棍 拼成一个正方形.你 不能折断 任何一根火柴棒,但你可以把它 ...

  6. 【473. 火柴拼正方形】

    来源:力扣(LeetCode) 描述 你将得到一个整数数组 matchsticks ,其中 matchsticks[i] 是第 i 个火柴棒的长度.你要用 所有的火柴棍 拼成一个正方形.你 不能折断 ...

  7. LeetCode 473. 火柴拼正方形(回溯)

    文章目录 1. 题目 2. 解题 1. 题目 还记得童话<卖火柴的小女孩>吗?现在,你知道小女孩有多少根火柴,请找出一种能使用所有火柴拼成一个正方形的方法. 不能折断火柴,可以把火柴连接起 ...

  8. 473. 火柴拼正方形

    还记得童话<卖火柴的小女孩>吗?现在,你知道小女孩有多少根火柴,请找出一种能使用所有火柴拼成一个正方形的方法.不能折断火柴,可以把火柴连接起来,并且每根火柴都要用到. 输入为小女孩拥有火柴 ...

  9. leetcode 473. Matchsticks to Square | 473. 火柴拼正方形(递归)

    题目 https://leetcode.com/problems/matchsticks-to-square/ 题解 看了 hint 之后,才有思路. 讨论区有个人说得好: This solution ...

最新文章

  1. 女朋友什么的都是浮云,代码才是真爱!
  2. 『HTML5制造仿JQuery作用』减速
  3. 基于Android和WI-FI通信的智能家居系统
  4. 加速 VR 渲染地狱难度进阶篇:降低图形 API 调用次数
  5. 计算机学院志愿公益活动,计算机学院开展学雷锋主题公益活动
  6. WebForm连接数据库实例
  7. android bool定义,android-R.bool
  8. 注意扩展方法的返回值类型
  9. C++中公有继承、保护继承、私有继承
  10. 近6年被引用次数最多的深度学习论文top100(附下载地址)
  11. Linux 镜像挂载
  12. 用于微信小程序的图文编辑器
  13. 信息论——信源信息量和信息熵
  14. CMD隐藏黑窗口运行
  15. PS 在PS中如何等比例放大缩小图片
  16. 英特尔神经计算棒_如何设置英特尔Movidius神经计算棒
  17. [语义分割]CTNet: Context-based Tandem Network for Semantic Segmentation
  18. JVM学习笔记(12) 垃圾回收-垃圾回收相关算法
  19. 下属被阿里挖角的那个早晨,我开始想念周鸿祎!
  20. 阿里系产品Xposed Hook检测机制原理分析

热门文章

  1. 【STM32】使用STM32cubeMX的库读写FLASH数据
  2. python从入门到实践:数据类型、文件处理
  3. Linux:打包压缩
  4. 【XR806开发板试用】TCP通信测试 Ping 命令测试
  5. 第二、三代基因组测序数据混合拼接软件综述
  6. python多项式拟合:np.polyfit 和 np.polyld
  7. drozer安装之夜深模拟器
  8. 低延时应用 服务器TurboBoost不可得兼?
  9. jsp铁路交通查询系统
  10. Tiny语言编译器简单介绍