[Swift]LeetCode473. 火柴拼正方形 | 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:

  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

时间: 2024-10-13 01:55:19

[Swift]LeetCode473. 火柴拼正方形 | Matchsticks to Square的相关文章

Leetcode 473.火柴拼正方形

火柴拼正方形 还记得童话<卖火柴的小女孩>吗?现在,你知道小女孩有多少根火柴,请找出一种能使用所有火柴拼成一个正方形的方法.不能折断火柴,可以把火柴连接起来,并且每根火柴都要用到. 输入为小女孩拥有火柴的数目,每根火柴用其长度表示.输出即为是否能用所有的火柴拼成正方形. 示例 1: 输入: [1,1,2,2,2] 输出: true 解释: 能拼成一个边长为2的正方形,每边两根火柴. 示例 2: 输入: [3,3,3,3,4] 输出: false 解释: 不能用所有火柴拼成一个正方形. 注意:

leetcode 火柴拼正方形 深搜

还记得童话<卖火柴的小女孩>吗?现在,你知道小女孩有多少根火柴,请找出一种能使用所有火柴拼成一个正方形的方法.不能折断火柴,可以把火柴连接起来,并且每根火柴都要用到. 输入为小女孩拥有火柴的数目,每根火柴用其长度表示.输出即为是否能用所有的火柴拼成正方形. 示例?1: 输入: [1,1,2,2,2] 输出: true 解释: 能拼成一个边长为2的正方形,每边两根火柴. 示例?2: 输入: [3,3,3,3,4] 输出: false 解释: 不能用所有火柴拼成一个正方形. 来源:力扣(LeetC

473. 火柴拼正方形

1 class Solution 2 { 3 public: 4 bool makesquare(vector<int>& nums) 5 { 6 if(nums.size()<4) return false; 7 int sum=0; 8 for(int i=0;i<nums.size();i++) sum+=nums[i]; 9 if(sum%4 != 0) return false; 10 sort(nums.rbegin(),nums.rend());//最神奇的一

[LeetCode] 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, a

LeetCode 473 - Matchsticks to Square - Medium (Python)

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, a

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, a

Leetcode: Matchsticks to Square &amp;&amp; Grammar: reverse an primative array

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, a

Matchsticks to Square &amp;&amp; Grammar: reverse an primative array

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, a

Leetcode题解 - 树、DFS部分简单题目代码+思路(700、671、653、965、547、473、46)

700. 二叉搜索树中的搜索 - 树 给定二叉搜索树(BST)的根节点和一个值. 你需要在BST中找到节点值等于给定值的节点. 返回以该节点为根的子树. 如果节点不存在,则返回 NULL. 思路: 二叉搜索树的特点为左比根小,右比根大.那么目标结点就有三种可能: 1. 和根一样大,那么直接返回根即可. 2. 比根的值小,那么应该再去次左子树中搜索. 3. 比根的值大,那么应该再次去右子树中搜索. 可以看到这就是一个递归的思路. class Solution: def searchBST(self