[Swift]LeetCode996. 正方形数组的数目 | Number of Squareful Arrays
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10390780.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
Given an array A
of non-negative integers, the array is squareful if for every pair of adjacent elements, their sum is a perfect square.
Return the number of permutations of A that are squareful. Two permutations A1
and A2
differ if and only if there is some index i
such that A1[i] != A2[i]
.
Example 1:
Input: [1,17,8]
Output: 2
Explanation:
[1,8,17] and [17,8,1] are the valid permutations.
Example 2:
Input: [2,2,2]
Output: 1
Note:
1 <= A.length <= 12
0 <= A[i] <= 1e9
给定一个非负整数数组 A
,如果该数组每对相邻元素之和是一个完全平方数,则称这一数组为正方形数组。
返回 A 的正方形排列的数目。两个排列 A1
和 A2
不同的充要条件是存在某个索引 i
,使得 A1[i] != A2[i]。
示例 1:
输入:[1,17,8] 输出:2 解释: [1,8,17] 和 [17,8,1] 都是有效的排列。
示例 2:
输入:[2,2,2] 输出:1
提示:
1 <= A.length <= 12
0 <= A[i] <= 1e9
1 class Solution { 2 var ans:Int = 0 3 func numSquarefulPerms(_ A: [Int]) -> Int { 4 var A = A 5 ans = 0 6 dfs(&A,0) 7 return ans 8 } 9 10 func dfs(_ A:inout [Int],_ k:Int) 11 { 12 var n:Int = A.count 13 if k == n 14 { 15 ans += 1 16 } 17 else 18 { 19 var set:Set<Int> = Set<Int>() 20 for i in k..<n 21 { 22 if (k == 0 || sq(A[k - 1] + A[i])) && !set.contains(A[i]) 23 { 24 set.insert(A[i]) 25 swap(&A, k, i) 26 dfs(&A, k + 1) 27 swap(&A, k, i) 28 } 29 } 30 } 31 } 32 33 func sq(_ n:Int) -> Bool 34 { 35 var r = Int(sqrt(Double(n))) 36 return r * r == n 37 } 38 39 func swap(_ A:inout [Int],_ i:Int,_ j:Int) 40 { 41 var temp:Int = A[i] 42 A[i] = A[j] 43 A[j] = temp 44 } 45 }
Runtime: 12 ms
1 class Solution { 2 var ans:Int = 0 3 func numSquarefulPerms(_ A: [Int]) -> Int { 4 var A = A 5 ans = 0 6 dfs(&A,0) 7 return ans 8 } 9 10 func dfs(_ A:inout [Int],_ k:Int) 11 { 12 var n:Int = A.count 13 if k == n 14 { 15 ans += 1 16 } 17 else 18 { 19 var set:Set<Int> = Set<Int>() 20 for i in k..<n 21 { 22 if (k == 0 || sq(A[k - 1] + A[i])) && !set.contains(A[i]) 23 { 24 set.insert(A[i]) 25 A.swapAt(k, i) 26 dfs(&A, k + 1) 27 A.swapAt(k, i) 28 } 29 } 30 } 31 } 32 33 func sq(_ n:Int) -> Bool 34 { 35 var r = Int(sqrt(Double(n))) 36 return r * r == n 37 } 38 }
12ms
1 class Solution { 2 func numSquarefulPerms(_ A: [Int]) -> Int { 3 var dict = [Int: Set<Int>]() 4 for (i1, num1) in A.enumerated() { 5 for (i2, num2) in A.enumerated() where i2 != i1 { 6 if matches(num1, num2) { 7 dict[num1, default: []].insert(num2) 8 } 9 } 10 } 11 12 var count = 0 13 fillData([], dict, A, &count) 14 15 return count 16 } 17 18 func fillData(_ array: [Int], _ dict: [Int: Set<Int>], _ ref: [Int], _ count: inout Int) { 19 if ref.count == 0 { 20 count += 1 21 return 22 } 23 24 let targetNums: Set<Int> 25 if array.count == 0 { 26 targetNums = Set(ref) 27 } else { 28 targetNums = dict[array.last!] ?? [] 29 } 30 31 for targetNum in targetNums { 32 if let i = ref.index(of: targetNum) { 33 var nextRef = ref 34 var nextArray = [nextRef.remove(at: i)] 35 fillData(nextArray, dict, nextRef, &count) 36 } 37 } 38 } 39 40 func matches(_ num1: Int, _ num2: Int) -> Bool { 41 let sqrted = Int(sqrt(Double(num1 + num2))) 42 return sqrted * sqrted == num1 + num2 43 } 44 }
18532 kb
1 class Solution { 2 var cache: [Int: Bool] = [0:true] 3 func numSquarefulPerms(_ A: [Int]) -> Int { 4 cache = [0:true] 5 6 var helpMap: [[Int]: [Int:Bool]] = [:] 7 var helpMap1: [[Int]: [Int:Bool]] = [:] 8 for i in (0..<A.count) { 9 guard helpMap[[A[i]]] == nil else { continue } 10 var vv = (helpMap[[A[i]]] ?? [:]) 11 vv[i] = true 12 helpMap[[A[i]]] = vv 13 } 14 for place in (1..<A.count) { 15 for i in (0..<A.count) { 16 for (key, value) in helpMap { 17 guard value[i] == nil else { continue } 18 if helpMap1[key + [A[i]]] == nil, comp(key.last! + A[i]) { 19 var vv = value 20 vv[i] = true 21 helpMap1[key + [A[i]]] = vv 22 } 23 } 24 } 25 helpMap = helpMap1 26 helpMap1 = [:] 27 } 28 return helpMap.keys.count 29 } 30 31 func comp(_ val: Int) -> Bool { 32 if let res = cache[val] { return res } 33 let sq = Double(val).squareRoot() 34 if Double(Int(sq)) == sq { 35 cache[val] = true 36 return true 37 } else { 38 cache[val] = false 39 return false 40 } 41 return false 42 } 43 }
18804 kb
1 class Solution { 2 var cache: [Int: Bool] = [0:true] 3 func permute(items: [Int]) -> [[Int]] { 4 // This is a scratch space for Heap's algorithm 5 var scratch = Array(items) 6 // This will accumulate our result 7 var result: [[Int]] = [] 8 var endPerm: [[Int]: Bool] = [:] 9 10 var allP = 0 11 var allowedP = 0 12 13 func heap(_ n: Int) { 14 let ar = Array(scratch[n..<scratch.count]) 15 if n < scratch.count, endPerm[ar] != nil { return } 16 endPerm[ar] = true 17 allP += 1 18 allowedP += 1 19 if n == 1 { 20 if checkAr(scratch) { 21 result.append(scratch) 22 } 23 endPerm[scratch] = true 24 return 25 } 26 27 for i in 0..<n-1 { 28 heap(n-1) 29 let j = (n%2 == 1) ? 0 : i 30 scratch.swapAt(j, n-1) 31 } 32 heap(n-1) 33 } 34 35 // Let's get started 36 heap(scratch.count) 37 print(allP, allowedP) 38 // And return the result we built up 39 return result 40 } 41 42 func numSquarefulPerms(_ A: [Int]) -> Int { 43 cache = [0:true] 44 45 var helpMap: [[Int]: [Int:Bool]] = [:] 46 var helpMap1: [[Int]: [Int:Bool]] = [:] 47 for i in (0..<A.count) { 48 guard helpMap[[A[i]]] == nil else { continue } 49 var vv = (helpMap[[A[i]]] ?? [:]) 50 vv[i] = true 51 helpMap[[A[i]]] = vv 52 } 53 print(helpMap) 54 for place in (1..<A.count) { 55 for i in (0..<A.count) { 56 for (key, value) in helpMap { 57 guard value[i] == nil else { continue } 58 if helpMap1[key + [A[i]]] == nil, comp(key.last! + A[i]) { 59 var vv = value 60 vv[i] = true 61 helpMap1[key + [A[i]]] = vv 62 } 63 } 64 } 65 helpMap = helpMap1 66 helpMap1 = [:] 67 } 68 69 return helpMap.keys.count 70 } 71 72 func checkAr(_ A: [Int]) -> Bool { 73 for i in (1..<A.count) { 74 let val = A[i-1] + A[i] 75 if !comp(val) { return false } 76 } 77 return true 78 } 79 80 func comp(_ val: Int) -> Bool { 81 if let res = cache[val] { return res } 82 let sq = Double(val).squareRoot() 83 if Double(Int(sq)) == sq { 84 cache[val] = true 85 return true 86 } else { 87 cache[val] = false 88 return false 89 } 90 return false 91 } 92 }