[Swift]LeetCode473. 火柴拼正方形 | Matchsticks to Square

[Swift]LeetCode473. 火柴拼正方形 | Matchsticks to Square,第1张

概述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 shoul

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]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,4]Output: falseExplanation: 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 to 10^9. The length of the given matchstick array will not exceed 15.

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

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

示例 1:

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

示例 2:

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

注意:

给定的火柴长度和在 0 到 10^9之间。 火柴数组的长度不超过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...all12         {13             var curSum:Int = 014             for j in 0...1515             {16                 if ((i >> j) & 1) == 117                 {18                     curSum += nums[j]19                 }20             }21             if curSum == target22             {23                 for mask in masks24                 {25                     if (mask & i) != 0 {continue}26                     var half:Int = mask | i27                     valIDHalf[half] = true28                     if valIDHalf[all ^ half] {return true}29                 }30                 masks.append(i)31             }32         }33         return false34     }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] == target15         }16         17         for i in 0 ..< sIDes.count {18             if sIDes[i] + nums[index] > target {19                 continue20             }21             sIDes[i] += nums[index]22             if dfs(nums,index + 1,&sIDes,target) {23                 return true24             }25             sIDes[i] -= nums[index]26         }27         return false28     }29 }
总结

以上是内存溢出为你收集整理的[Swift]LeetCode473. 火柴拼正方形 | Matchsticks to Square全部内容,希望文章能够帮你解决[Swift]LeetCode473. 火柴拼正方形 | Matchsticks to Square所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1020025.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存